Was this page helpful?
Additional feedback?
1500 characters remaining
String Class
Collapse the table of content
Expand the table of content

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.



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.





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


Assembly: Mscorlib (in Mscorlib.dll)

See Also

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

© 2015 Microsoft