Words and Text: Using String Variables to Organize Words

In this lesson, you will learn how to use the String data type to represent words and text.

The previous lesson showed how to use variables to store data in a 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 here.

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 here. This is also known as concatenation.

Dim aString As String = "using string"
Dim bString As String = "variables"
Dim cString As String = ""
cString = aString & bString

The previous example declares three String variables and respectively assigns "using string" and "variables" 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 using stringvariables because there is no space at the end of aString or at the start of bString. The two strings are just 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 here.

Dim aString As String = "using string"
Dim bString As String = "variables"
Dim cString As String = ""
cString = aString & " " & bString

The text that is contained in cString now reads as using string variables.

Try It!

To join strings

  1. On the File menu, click New Project.

  2. In the New Project dialog box:

    1. In the Templates pane, click Windows Application.

    2. In the Name box, type Concatenation.

    3. Click OK.

    A new Windows Forms project opens.

  3. Double-click the form to open the Code Editor.

  4. In the Form1.Load event procedure, declare four string variables and assign the string values, as shown here:

    Dim aString As String = "Concatenating"
    Dim bString As String = "Without"
    Dim cString As String = "With"
    Dim dString As String = "Spaces"
    
  5. Add the following code to concatenate the strings and display the results:

    ' Displays "ConcatenatingWithoutSpaces".
    MsgBox(aString & bString & dString)
    
    ' Displays "Concatenating With Spaces".
    MsgBox(aString & " " & cString & " " & dString)
    
  6. Press F5 to run your program.

    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.

Next Steps

In this lesson, you learned how to declare and assign strings, and how to join strings with the concatenation operator &. In the next lesson, Arrays: Variables That Represent More Than One Value, you will learn about how to create variables to store groups of similar items.

Next Lesson: Arrays: Variables That Represent More Than One Value

See Also

Tasks

Arrays: Variables That Represent More Than One Value

Representing Words, Numbers, and Values with Variables

Concepts

Introduction to the Visual Basic Programming Language