-
Mar 7th, 2016, 12:59 AM
#1
Thread Starter
Addicted Member
[RESOLVED] String Help
Hey all!
In a console app, you can condense strings by using this code on line 5:
vb Code:
Dim R As New Random()
Dim l As Int32 = R.Next(10)
Dim m As Int32 = R.Next(10)
Dim n As Int32 = R.Next(10)
Console.WriteLine("L is {0} and M is {1} and N is {2}.", l, m, n)
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:
'' How do I go from this:
Dim s As String = "L is " & l & " and M is " & m & " and N is " & n & "."
'' To this:
Dim s As String = ("L is {0} and M is {1} and N is {2}.", l, m, n)
Thanks!
Nic
-
Mar 7th, 2016, 02:17 AM
#2
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:
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:
Dim s As String = $"L is {l} and M is {m} and N is {n}."
-
Mar 8th, 2016, 12:37 AM
#3
Thread Starter
Addicted Member
Re: String Help
Thank you! Both methods work great.
-
Mar 8th, 2016, 02:54 AM
#4
Hyperactive Member
Re: String Help
Originally Posted by jmcilhinney
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:
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.
-
Mar 8th, 2016, 03:39 AM
#5
Re: String Help
Originally Posted by Maverickz
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|