Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable substitution into strings

Tags:

.net

vb.net

Can I do something like "%s said %s blah.", $name, $blah; in VB.NET?

It's getting painful writing name & "said" & blah & "blah".

like image 448
Harrison Avatar asked Feb 05 '10 21:02

Harrison


People also ask

How do you pass a variable to a string in PowerShell?

PowerShell has another option that is easier. You can specify your variables directly in the strings. $message = "Hello, $first $last." The type of quotes you use around the string makes a difference.

How do you write a variable to a string in Python?

To create a string, put the sequence of characters inside either single quotes, double quotes, or triple quotes and then assign it to a variable. You can look into how variables work in Python in the Python variables tutorial. For example, you can assign a character 'a' to a variable single_quote_character .


2 Answers

In VB.NET 14 (for VS2015), you can use string interpolation:

Dim newString As String = $"{name} said {blah} blah."
like image 65
Mark Hurd Avatar answered Oct 13 '22 10:10

Mark Hurd


Yup, use String.Format:

Dim newString As String = String.Format("{0} said {1} blah.", name, blah)
like image 21
Gavin Miller Avatar answered Oct 13 '22 12:10

Gavin Miller