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.
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.
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.
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.
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.
The text that is contained in cString now reads as using string variables.
To join strings
On the File menu, click New Project.
In the New Project dialog box:
In the Templates pane, click Windows Application.
In the Name box, type Concatenation.
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 here:
Add the following code to concatenate the strings and display the results:
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.
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