Flylib.com

Books Software

 
 
 

1.1 Combine Strings


1.1 Combine Strings

Problem

You need to join two strings or insert a string into another string.

Solution

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.

Discussion

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"
Note 

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 Remove method, which enables you to delete a specified number of characters in the middle of a string:

FullName = FullName.Remove(5, MiddleName.Length) ' FullName is now "Bill Jones"



1.2 Retrieve a Portion of a String

Problem

You need to retrieve a portion of a string based on its position and length.

Solution

Use the String.Substring method.

Discussion

The String.Substring method requires two integer parameters, a startIndex and a length . As with all string indexes, startIndex is zero-based (in other words, the first character in the string is designated as character 0).

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

characters

from the right of a string. ' This is equivalent to Right(MyString, x) in VB 6. NewString = MyString.SubString(MyString.Length - x, x)



1.3 Create a String Consisting of a Repeated Character

Problem

You need to quickly create a string that consists of a single character repeated multiple times (for example, "------------").

Solution

Use the overloaded String constructor that accepts a single character and a repetition number.

Discussion

The following code creates a string made up of 100 dash characters . Note that in order to use the nondefault String constructor, you must use the New keyword when you declare the string.

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.