Condividi tramite


String Data Type (Visual Basic)

Holds sequences of unsigned 16-bit (2-byte) code points that range in value from 0 through 65535. Each code point, or character code, represents a single Unicode character. A string can contain from 0 to approximately two billion (2 ^ 31) Unicode characters.

Remarks

Use the String data type to hold multiple characters without the array management overhead of Char(), an array of Char elements.

The default value of String is Nothing (a null reference). Note that this is not the same as the empty string (value "").

Unicode Characters

The first 128 code points (0–127) of Unicode correspond to the letters and symbols on a standard U.S. keyboard. These first 128 code points are the same as those the ASCII character set defines. The second 128 code points (128–255) represent special characters, such as Latin-based alphabet letters, accents, currency symbols, and fractions. Unicode uses the remaining code points (256-65535) for a wide variety of symbols. This includes worldwide textual characters, diacritics, and mathematical and technical symbols.

You can use methods such as IsDigit and IsPunctuation on an individual character in a String variable to determine its Unicode classification.

Format Requirements

You must enclose a String literal within quotation marks (" "). If you must include a quotation mark as one of the characters in the string, you use two contiguous quotation marks (""). The following example illustrates this.

Dim j As String = "Joe said ""Hello"" to me."
Dim h As String = "Hello"
' The following messages all display the same thing:
' "Joe said "Hello" to me."
MsgBox(j)
MsgBox("Joe said " & """" & h & """" & " to me.")
MsgBox("Joe said """ & h & """ to me.")

Note that the contiguous quotation marks that represent a quotation mark in the string are independent of the quotation marks that begin and end the String literal.

String Manipulations

Once you assign a string to a String variable, that string is immutable, which means you cannot change its length or contents. When you alter a string in any way, Visual Basic creates a new string and abandons the previous one. The String variable then points to the new string.

You can manipulate the contents of a String variable by using a variety of string functions. The following example illustrates the Left function

Dim S As String = "Database"
' The following statement sets S to a new string containing "Data".
S = Microsoft.VisualBasic.Left(S, 4)

A string created by another component might be padded with leading or trailing spaces. If you receive such a string, you can use the Trim, LTrim, and RTrim functions to remove these spaces.

For more information about string manipulations, see Strings in Visual Basic.

Programming Tips

  • Negative Numbers. Remember that the characters held by String are unsigned and cannot represent negative values. In any case, you should not use String to hold numeric values.

  • Interop Considerations. If you are interfacing with components not written for the .NET Framework, for example Automation or COM objects, remember that string characters have a different data width (8 bits) in other environments. If you are passing a string argument of 8-bit characters to such a component, declare it as Byte(), an array of Byte elements, instead of String in your new Visual Basic code.

  • Type Characters. Appending the identifier type character $ to any identifier forces it to the String data type. String has no literal type character. However, the compiler treats literals enclosed in quotation marks (" ") as String.

  • Framework Type. The corresponding type in the .NET Framework is the System.String class.

See Also

Tasks

How to: Call a Windows Function that Takes Unsigned Types (Visual Basic)

Reference

Data Type Summary (Visual Basic)

System.String

Char Data Type (Visual Basic)

Type Conversion Functions (Visual Basic)

Conversion Summary (Visual Basic)

Concepts

Efficient Use of Data Types (Visual Basic)