Represents a static string of characters.
Object
String
[Visual Basic] NotInheritable Public Class String Implements IComparable, ICloneable, IConvertible [C#] public sealed class String : IComparable, ICloneable, IConvertible [C++] public __gc __sealed class String : public IComparable, ICloneable, IConvertible [JScript] public class String implements IComparable, ICloneable, IConvertible
Methods are provided for comparing, formatting, and manipulating strings, but note that an instance of String cannot be modified once it has been created. Indeed, methods such as Remove, Replace, and Trim actually create a new string based on the current string. The StringBuilder can be used to create or modify strings dynamically.
Character positions (indices) within a string are 0-based, as with arrays. Comparison and search procedures are case-sensitive and use the CurrentCulture of the current thread unless otherwise specified.
When conducting textual comparisons one should use the Compare and Equals methods provided by String. These operators compare String objects for reference equality, unless they are overloaded by the language/compiler. However, the Environment makes it possible for String objects that are assigned literal values to be compared with the relational operators. This works for string literals only. A dynamically created string (one obtained via StringBuilder or one that has been created via the String methods such as Remove, Replace, and Trim) cannot be textually compared with the relational operators. The Compare and Equals methods must be used in this case, and should be used in general.
Namespace: System
Assembly: mscorlib.dll
The following examples are for illustrative purposes only; they will not run as specified in the comments. The first set of examples shows the use of relational operators.
Managed C++:
Console::WriteLine(L"twins" == L"twins"); // THIS IS TRUE String *SL = L"twins"; // same as new Console::WriteLine(SL == L"twins"); // THIS IS TRUE String *TL = new String(L"twins"); Console::WriteLine(SL == TL); // THIS IS TRUE SL = SL->Remove(4,1); // remove 's' from "twins" SL = SL + L"s"; // append 's' to "twin" Console::WriteLine(SL == TL); // THIS IS FALSE StringBuilder *UL = new StringBuilder(L"twins"); SL = UL->ToString(); Console::WriteLine(SL == TL); // THIS IS FALSE Visual Basic: Console.WriteLine("twins" = "twins") ' THIS IS TRUE Dim SL As String SL = "twins" ' Same as: Set SL = New Console.WriteLine(SL = "twins") ' THIS IS TRUEDim TL As String Set TL = New String("twins") Console.WriteLine(SL = TL) ' THIS IS TRUE SL = SL.Remove(4,1) ' Remove "s" from "twins" SL = SL + "s"' Append "s" to "twin" Console.WriteLine(SL = TL) ' THIS IS FALSE Dim UL As StringBuilder Set UL = New StringBuilder("twins") SL = UL.ToString() Console.WriteLine(SL = TL) ' THIS IS FALSE The following example illustrates the use of String.Equals and Compare. Managed C++ String *SL = L"twins"; Console::WriteLine(SL->Equals(L"twins")); // THIS IS TRUE String *TL = new String(L"twins"); Console::WriteLine(0 == String::Compare(SL,TL)); // THIS IS TRUE SL = SL->Remove(4,1); // Remove 's' from "twins" SL = SL + L"s";// Append 's' to "twin" Console::WriteLine(SL->Equals(TL)); // THIS IS TRUE StringBuilder *UL = new StringBuilder(L"twins"); SL = UL->ToString(); Console::WriteLine(0 == String::Compare(SL,TL)); // THIS IS TRUE Visual Basic: Dim SL As String SL = "twins" Console.WriteLine(SL.Equals("twins")) ' THIS IS TRUE Dim TL As String TL = "twins" Console.WriteLine(0 = System.String.Compare(SL,TL)) ' THIS IS TRUE SL = SL.Remove(4,1)' Remove "s" from "twins" SL = SL + "s" ' Append "s" to "twin" Console.WriteLine(SL.Equals(TL)) ' THIS IS TRUE Dim UL As StringBuilder Set UL = New StringBuilder("twins") SL = UL.ToString() Console.WriteLine(0 = System.String.Compare(SL,TL)) ' THIS IS TRUE
String Members | System Namespace | IComparable | ICloneable | IConvertible | Environment | Object | StringBuilder | CultureInfo