You need to join two strings or insert a string into another string.
Use the & operator to add one string to the end of another. Use the String.Insert method to insert a string in the middle of another string.
You can join strings together using the & or + operator.
Dim FirstName As String = "Bill" Dim LastName As String = "Jones" Dim FullName As String FullName = FirstName & " " & LastName ' FullName is now "Bill Jones"
Although the + operator can be used to join strings in the same way as the & operator, it's not recommended. If you use the + operator for concatenation and one of the values in your expression isn't a string, Visual Basic .NET will attempt to convert both values to a Double and perform numeric addition. However, if you use the & operator, Visual Basic .NET will attempt to convert any nonstring value in the expression to a string and perform concatenation. Thus, the & operator is preferred for concatenation because it's unambiguous—it always does string concatenation, regardless of the data types you use.
Strings also provide an Insert method that enables you to place a substring in the middle of another string. It requires a startIndex integer parameter that specifies where the string should be inserted.
Dim FullName As String = "Bill Jones" Dim MiddleName As String = "Neuhaus " ' Insert MiddleName at position 5 in FullName. FullName = FullName.Insert(5, MiddleName) ' FullName is now "Bill Neuhaus Jones"
Incidentally, you can also use the complementary
method, which enables you to delete a specified number of
FullName = FullName.Remove(5, MiddleName.Length) ' FullName is now "Bill Jones"
You need to retrieve a portion of a string based on its position and length.
Use the String.Substring method.
method requires two integer parameters, a
. As with all string indexes,
Dim FullName As String = "Bill Jones" Dim FirstName As String ' Retrieve the 4-character substring starting at 0. FirstName = FullName.Substring(0, 4) ' FirstName is now "Bill"
Optionally, you can omit the length parameter to take a substring that continues to the end of the string:
Dim FullName As String = "Bill Jones" Dim LastName As String ' Retrieve the substring starting at 5, and continuing to the ' end of the string. LastName = FullName.Substring(5) ' LastName is now "Jones"
Visual Basic .NET includes the legacy Left and Right functions, but they're not recommended. Instead, you can use the Substring method in conjunction with the Length property to provide the same functionality. The code snippet below outlines this approach.
' Retrieve x characters from the left of a string. ' This is equivalent to Left(MyString, x) in VB 6. NewString = MyString.SubString(0, x) ' Retrieve x
charactersfrom the right of a string. ' This is equivalent to Right(MyString, x) in VB 6. NewString = MyString.SubString(MyString.Length - x, x)
You need to quickly create a string that consists of a single character repeated multiple times (for example, "------------").
Use the overloaded String constructor that accepts a single character and a repetition number.
The following code creates a string made up of 100 dash
Dim Dashes As New String("-"c, 100)
When specifying the character to repeat, you can append the letter c after the string to signal that the quoted text represents a Char , as required by this constructor, not a String . This is required if you have Option Strict enabled, which disables implicit conversions between Char and String instances.
You could also perform the same task by using string concatenation in a loop. However, that approach would be much slower.