String Data Type (Visual Basic) 

Holds sequences of unsigned 16-bit (2-byte) code points ranging 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 2 billion (2 ^ 31) Unicode characters.

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 "").

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, including worldwide textual characters, diacritics, and mathematical and technical symbols.

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

You must enclose a String literal within quotation marks (" "). If you need to 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("Joe said " & """" & h & """" & " to me.")
MsgBox("Joe said """ & h & """ to me.")

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

Once you assign a string to a String variable, that string is immutable, meaning 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 using a variety of string functions. The following example illustrates the Left Function (Visual Basic).

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 on string manipulations, see Strings in Visual Basic.

  • Negative Numbers. Keep in mind 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, keep in mind 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 within quotation marks (" ") as String.

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