In this lesson, you will learn how to use the String data type to represent words and text.
In the previous lesson, you learned how to use variables to store data in your program, and that each variable must be of the appropriate type for the data that it will store. In this lesson, you will learn more about the String data type, which is used to store text.
What Is a String?
A string is any series of text characters, such as letters, numbers, special characters, and spaces. Strings can be human-readable phrases or sentences, such as "The quick brown fox jumps over the lazy dog," or an apparently unintelligible combination, such as "@#fTWRE^3 35Gert".
String variables are created just as other variables: by first declaring the variable and assigning it a value, as shown below.
Dim aString As String = "This is a string"
When assigning actual text (also called a string literal) to a String variable, the text must be enclosed in quotation marks (""). You can also use the = character to assign one String variable to another String variable, as shown in this example.
Dim aString As String = "This is a string"
...
Dim bString As String = ""
bString = aString
The previous code sets the value of bString to the same value as aString (This is a string).
You can use the ampersand (&)character to sequentially combine two or more strings into a new string, as shown below.
Dim aString As String = "Across the Wide"
Dim bString As String = "Missouri"
Dim cString As String = ""
cString = aString & bString
The previous example declares three String variables and respectively assigns "Across the Wide" and "Missouri" to the first two, and then assigns the combined values of the first two to the third variable. What do you think the value of cString is? You might be surprised to learn that the value is Across the WideMissouri because there is no space at the end of aString or at the beginning of bString. The two strings are simply joined together. If you want to add spaces or anything else between two strings, you must do so with a string literal, such as " ", as shown below.
Dim aString As String = "Across the Wide"
Dim bString As String = "Missouri"
Dim cString As String = ""
cString = aString & " " & bString
The text contained in cString now reads as Across the Wide Missouri.
Try It!
To join strings
On the File menu, click NewProject.
In the New Project dialog box:
In the Templates pane, click Windows Application.
In the Name box, type Concatenation.
Click OK.
A new Windows Forms project opens.
Double-click the form to open the Code Editor.
In the Form1.Load event procedure, declare four string variables and assign the string values, as shown below:
Dim aString As String = "Concatenating"
Dim bString As String = "Without"
Dim cString As String = "With"
Dim dString As String = "Spaces"
Add the following code to concatenate the strings and display the results:
MsgBox(aString & bString & dString) ' Displays "ConcatenatingWithoutSpaces"
...
MsgBox(aString & " " & cString & " " & dString)
' Displays "Concatenating With Spaces"
The text displayed in the message box is the result of joining the string variables that were assigned in a previous step. In the first box, the strings are joined together without spaces. In the second, spaces are explicitly inserted between each string.