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.

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. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值