Results 1 to 5 of 5

Thread: [RESOLVED] String Help

  1. #1

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Resolved [RESOLVED] String Help

    Hey all!

    In a console app, you can condense strings by using this code on line 5:

    vb Code:
    1. Dim R As New Random()
    2. Dim l As Int32 = R.Next(10)
    3. Dim m As Int32 = R.Next(10)
    4. Dim n As Int32 = R.Next(10)
    5. Console.WriteLine("L is {0} and M is {1} and N is {2}.", l, m, n)
    6. Console.ReadLine()

    But why can't you create a normal string that way? Is the long way the only other way to do it?

    vb Code:
    1. '' How do I go from this:
    2. Dim s As String = "L is " & l & " and M is " & m & " and N is " & n & "."
    3. '' To this:
    4. Dim s As String = ("L is {0} and M is {1} and N is {2}.", l, m, n)

    Thanks!

    Nic

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,008

    Re: String Help

    That is not some magic string thing. That's the implementation of Console.WriteLine that is doing that. If you want to do that to create a String without writing to the console then you call String.Format, which is implemented in a similar way:
    vb.net Code:
    1. Dim s As String = String.Format("L is {0} and M is {1} and N is {2}.", l, m, n)

    That said, there is a magic string thing now in VB 2015. It's called String Interpolation. Now you can create a String even more easily than with String.Format:
    vb.net Code:
    1. Dim s As String = $"L is {l} and M is {m} and N is {n}."

  3. #3

    Thread Starter
    Addicted Member NinjaNic's Avatar
    Join Date
    Dec 2013
    Location
    Earth
    Posts
    230

    Re: String Help

    Thank you! Both methods work great.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2004
    Posts
    457

    Re: String Help

    Quote Originally Posted by jmcilhinney View Post
    That said, there is a magic string thing now in VB 2015. It's called String Interpolation. Now you can create a String even more easily than with String.Format:
    vb.net Code:
    1. Dim s As String = $"L is {l} and M is {m} and N is {n}."
    Heh I upgraded to 2015 a few months ago and I didn't know about that yet. That is nifty. Guess I should read the documentation more often.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,008

    Re: String Help

    Quote Originally Posted by Maverickz View Post
    Heh I upgraded to 2015 a few months ago and I didn't know about that yet. That is nifty. Guess I should read the documentation more often.
    With each new version, it's not a bad idea to at least check out the What's New article for VS and VB. That'll cover big things like this.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width