This documentation is archived and is not being maintained.

String Class

Represents text; that is, a series of Unicode characters.

For a list of all members of this type, see String Members.


[Visual Basic]
NotInheritable Public Class String
   Implements IComparable, ICloneable, IConvertible, IEnumerable
public sealed class String : IComparable, ICloneable,
   IConvertible, IEnumerable
public __gc __sealed class String : public IComparable,
   ICloneable, IConvertible, IEnumerable
class String implements IComparable, ICloneable,
   IConvertible, IEnumerable

Thread Safety

This type is safe for multithreaded operations.


A string is a sequential collection of Unicode characters, typically used to represent text, while a String is a sequential collection of System.Char objects that represents a string. The value of the String is the content of the sequential collection, and the value is immutable.

A String is called immutable because its value cannot be modified once it has been created. Methods that appear to modify a String actually return a new String containing the modification. If it is necessary to modify the actual contents of a string-like object, use the System.Text.StringBuilder class.

Each Unicode character in a string is defined by a Unicode scalar value, also called a Unicode code point or the ordinal (numeric) value of the Unicode character. Each code point is encoded using UTF-16 encoding, and the numeric value of each element of the encoding is represented by a Char. The resulting collection of Char objects constitutes the String.

A single Char usually represents a single code point; that is, the numeric value of the Char equals the code point. However, a code point might require more than one encoded element. For example, a Unicode supplementary code point (a surrogate pair) is encoded with two Char objects.

An index is the position of a Char, not a Unicode character, in a String. An index is a zero-based, nonnegative number starting from the first position in the string, which is index position zero. Consecutive index values might not correspond to consecutive Unicode characters because a Unicode character might be encoded as more than one Char. To work with each Unicode character instead of each Char, use the System.Globalization.StringInfo class.

Members of String perform either an ordinal or linguistic operation on a String. An ordinal operation acts on the numeric value of each Char object. A linguistic operation acts on the value of the String taking into account culture-specific casing, sorting, formatting, and parsing rules. Linguistic operations execute in the context of an explicitly declared culture or the implicit current culture. For more information about the current culture, see the CultureInfo.CurrentCulture topic.

Casing rules determine how to change a Unicode character between one case and another; for example, from lowercase to uppercase.

Formatting rules determine how to convert a value to its string representation, while parsing rules determine how to convert a string representation to a value.

Sort rules determine the alphabetic order of Unicode characters and how two strings compare to each other. For example, the Compare method performs a linguistic comparison while the CompareOrdinal method performs an ordinal comparison. Consequently, if the current culture is U.S. English, the Compare method considers 'a' less than 'A' while the CompareOrdinal method considers 'a' greater than 'A'.

The .NET Framework supports word, string, and ordinal sort rules. A word sort performs a culture-sensitive comparison of strings in which certain nonalphanumeric Unicode characters might have special weights assigned to them. For example, the hyphen ("-") might have a very small weight assigned to it so that "coop" and "co-op" appear next to each other in a sorted list. A string sort is similar to a word sort, except that there are no special cases and all nonalphanumeric symbols come before all alphanumeric Unicode characters. An ordinal sort compares strings based on the numeric value of each Char in the string. For more information about word, string, and ordinal sort rules, see the System.Globalization.CompareOptions topic.

Comparison and search procedures are case-sensitive by default and use the culture associated with the current thread unless specified otherwise. By definition, any string, including the empty string (""), compares greater than a null reference, and two null references compare equal to each other.

If your application makes security decisions based on the result of a comparison or case change operation, then the operation should use the invariant culture to ensure the result is not affected by the value of the current culture. For more information, see the CultureInfo.InvariantCulture topic.

Use Compare, CompareOrdinal, CompareTo, Equals, EndsWith, and StartsWith for comparisons. Use IndexOf, IndexOfAny, LastIndexOf, and LastIndexOfAny to obtain the index of a substring or Unicode character in a string. Use Copy and CopyTo to copy a string or substring to another string or an array of Char.

Use Substring and Split to create one or more new strings from portions of an original string, and Concat and Join to create a new string from one or more substrings. Use Insert, Replace, Remove, PadLeft, PadRight, Trim, TrimEnd, and TrimStart to modify all or part of a string.

Use ToLower and ToUpper to change the case of Unicode characters in a string. Use Format to replace one or more placeholders in a string with the string representation of one or more values. Use the Length property to obtain the number of Char objects in a string, and the Chars property to access the actual Char objects in a string.

String implements the IComparable, ICloneable, IConvertible, and IEnumerable interfaces. Use the Convert class for conversions instead of this type's explicit interface member implementation of IConvertible.


Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

String Members | System Namespace | IComparable | ICloneable | IConvertible | IEnumerable | System.Text.StringBuilder | CultureInfo | Formatting Overview