Class string
Represents text as a sequence of UTF-16 code units.
Inherited Members
Namespace: System
Assembly: mscorlib.dll
Syntax
public sealed string : IComparable, IEnumerable
Constructors
String(char, int)
Initializes a new instance of the String class to the value indicated by a specified Unicode character repeated a specified number of times.
Declaration
public String(char c, int count)
Parameters
Type | Name | Description |
---|---|---|
char | c | A Unicode character. |
int | count | The number of times c occurs. |
String(char[])
Initializes a new instance of the String class to the value indicated by an array of Unicode characters.
Declaration
public String(char[] value)
Parameters
Type | Name | Description |
---|---|---|
char[] | value | An array of Unicode characters. |
String(char[], int, int)
Initializes a new instance of the String class to the value indicated by an array of Unicode characters, a starting character position within that array, and a length.
Declaration
public String(char[] value, int startIndex, int length)
Parameters
Type | Name | Description |
---|---|---|
char[] | value | An array of Unicode characters. |
int | startIndex | The starting position within value. |
int | length | The number of characters within value to use. |
Fields
Empty
Represents the empty string. This field is read-only.
Declaration
public static readonly string Empty
Field Value
Type | Description |
---|---|
string |
Properties
this[int]
Gets the Char object at a specified position in the current String object.
Declaration
public char this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
int | index | A position in the current string. |
Property Value
Type | Description |
---|---|
char | The object at position index. |
Length
Gets the number of characters in the current String object.
Declaration
public int Length { get; }
Property Value
Type | Description |
---|---|
int | The number of characters in the current string. |
Methods
Compare(string, string)
Compares two specified String objects and returns an integer that indicates their relative position in the sort order.
Declaration
public static int Compare(string strA, string strB)
Parameters
Type | Name | Description |
---|---|---|
string | strA | The first string to compare. |
string | strB | The second string to compare. |
Returns
Type | Description |
---|---|
int | A 32-bit signed integer that indicates the lexical relationship between the two comparands. |
CompareTo(object)
Compares this instance with a specified Object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified Object.
Declaration
public int CompareTo(object value)
Parameters
Type | Name | Description |
---|---|---|
object | value | An object that evaluates to a String. |
Returns
Type | Description |
---|---|
int | A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the value parameter. |
CompareTo(string)
Compares this instance with a specified String object and indicates whether this instance precedes, follows, or appears in the same position in the sort order as the specified string.
Declaration
public int CompareTo(string strB)
Parameters
Type | Name | Description |
---|---|---|
string | strB | The string to compare with this instance. |
Returns
Type | Description |
---|---|
int | A 32-bit signed integer that indicates whether this instance precedes, follows, or appears in the same position in the sort order as the strB parameter. |
Concat(object)
Creates the string representation of a specified object.
Declaration
public static string Concat(object arg0)
Parameters
Type | Name | Description |
---|---|---|
object | arg0 | The object to represent, or null. |
Returns
Type | Description |
---|---|
string | The string representation of the value of arg0, or String.Empty if arg0 is null. |
Concat(object, object)
Concatenates the string representations of two specified objects.
Declaration
public static string Concat(object arg0, object arg1)
Parameters
Type | Name | Description |
---|---|---|
object | arg0 | The first object to concatenate. |
object | arg1 | The second object to concatenate. |
Returns
Type | Description |
---|---|
string | The concatenated string representations of the values of arg0 and arg1. |
Concat(object, object, object)
Concatenates the string representations of three specified objects.
Declaration
public static string Concat(object arg0, object arg1, object arg2)
Parameters
Type | Name | Description |
---|---|---|
object | arg0 | The first object to concatenate. |
object | arg1 | The second object to concatenate. |
object | arg2 | The third object to concatenate. |
Returns
Type | Description |
---|---|
string | The concatenated string representations of the values of arg0, arg1 and arg2. |
Concat(params object[])
Concatenates the string representations of the elements in a specified Object array.
Declaration
public static string Concat(params object[] args)
Parameters
Type | Name | Description |
---|---|---|
object[] | args | An object array that contains the elements to concatenate. |
Returns
Type | Description |
---|---|
string | The concatenated string representations of the values of the elements in args. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
Concat(string, string)
Concatenates two specified instances of String.
Declaration
public static string Concat(string str0, string str1)
Parameters
Type | Name | Description |
---|---|---|
string | str0 | The first string to concatenate. |
string | str1 | The second string to concatenate. |
Returns
Type | Description |
---|---|
string | The concatenation of str0 and str1. |
Concat(string, string, string)
Concatenates three specified instances of String.
Declaration
public static string Concat(string str0, string str1, string str2)
Parameters
Type | Name | Description |
---|---|---|
string | str0 | The first string to concatenate. |
string | str1 | The second string to concatenate. |
string | str2 | The third string to concatenate. |
Returns
Type | Description |
---|---|
string | The concatenation of str0, str1 and str2. |
Concat(string, string, string, string)
Concatenates four specified instances of String.
Declaration
public static string Concat(string str0, string str1, string str2, string str3)
Parameters
Type | Name | Description |
---|---|---|
string | str0 | The first string to concatenate. |
string | str1 | The second string to concatenate. |
string | str2 | The third string to concatenate. |
string | str3 | The fourth string to concatenate. |
Returns
Type | Description |
---|---|
string | The concatenation of str0, str1, str2 and str3. |
Concat(params string[])
Concatenates the elements of a specified String array.
Declaration
public static string Concat(params string[] values)
Parameters
Type | Name | Description |
---|---|---|
string[] | values | An array of string instances. |
Returns
Type | Description |
---|---|
string | The concatenated elements of values. |
Contains(string)
Returns a value indicating whether a specified substring occurs within this string.
Declaration
public bool Contains(string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to seek. |
Returns
Type | Description |
---|---|
bool | true if the |
EndsWith(string)
Determines whether the end of this string instance matches the specified string.
Declaration
public bool EndsWith(string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to compare to the substring at the end of this instance. |
Returns
Type | Description |
---|---|
bool | true if |
Equals(object)
Determines whether this instance and a specified object, which must also be a String object, have the same value.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
object | obj | The string to compare to this instance. |
Returns
Type | Description |
---|---|
bool | true if obj is a String and its value is the same as this instance; otherwise, false. If obj is null, the method returns false. |
Overrides
Equals(string, string)
Determines whether two specified String objects have the same value.
Declaration
public static bool Equals(string a, string b)
Parameters
Type | Name | Description |
---|---|---|
string | a | The first string to compare, or null. |
string | b | The second string to compare, or null. |
Returns
Type | Description |
---|---|
bool | true if the value of a is the same as the value of b; otherwise, false. If both a and b are null, the method returns true. |
Format(string, params object[])
Replaces the format items in a string with the string representations of corresponding objects in a specified array.
Declaration
public static string Format(string format, params object[] args)
Parameters
Type | Name | Description |
---|---|---|
string | format | A composite format string |
object[] | args | An object array that contains zero or more objects to format. |
Returns
Type | Description |
---|---|
string | A copy of format in which the format items have been replaced by the string representation of the corresponding objects in args. |
GetEnumerator()
Not supported in NanoFramework
Return an enumerator that iterate on each char of the string.
Declaration
public IEnumerator GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator | An IEnumerator object that can be used to iterate through the collection. |
IndexOf(char)
Reports the zero-based index of the first occurrence of the specified Unicode character in this string.
Declaration
public int IndexOf(char value)
Parameters
Type | Name | Description |
---|---|---|
char | value | A Unicode character to seek. |
Returns
Type | Description |
---|---|
int | The zero-based index position of value if that character is found, or -1 if it is not. |
IndexOf(char, int)
Reports the zero-based index of the first occurrence of the specified Unicode character in this string. The search starts at a specified character position.
Declaration
public int IndexOf(char value, int startIndex)
Parameters
Type | Name | Description |
---|---|---|
char | value | A Unicode character to seek. |
int | startIndex | The search starting position. |
Returns
Type | Description |
---|---|
int | The zero-based index position of value from the start of the string if that character is found, or -1 if it is not. |
IndexOf(char, int, int)
Reports the zero-based index of the first occurrence of the specified character in this instance. The search starts at a specified character position and examines a specified number of character positions.
Declaration
public int IndexOf(char value, int startIndex, int count)
Parameters
Type | Name | Description |
---|---|---|
char | value | A Unicode character to seek. |
int | startIndex | The search starting position. |
int | count | The number of character positions to examine. |
Returns
Type | Description |
---|---|
int | The zero-based index position of value if that character is found, or -1 if it is not. |
IndexOf(string)
Reports the zero-based index of the first occurrence of the specified string in this instance.
Declaration
public int IndexOf(string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to seek. |
Returns
Type | Description |
---|---|
int | The zero-based index position of value if that string is found, or -1 if it is not. If value is String.Empty, the return value is 0. |
IndexOf(string, int)
Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position.
Declaration
public int IndexOf(string value, int startIndex)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to seek. |
int | startIndex | The search starting position. |
Returns
Type | Description |
---|---|
int | The zero-based index position of value from the start of the current instance if that string is found, or -1 if it is not. If value is String.Empty, the return value is startIndex. |
IndexOf(string, int, int)
Reports the zero-based index of the first occurrence of the specified string in this instance. The search starts at a specified character position and examines a specified number of character positions.
Declaration
public int IndexOf(string value, int startIndex, int count)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to seek. |
int | startIndex | The search starting position. |
int | count | The number of character positions to examine. |
Returns
Type | Description |
---|---|
int | The zero-based index position of value from the start of the current instance if that string is found, or -1 if it is not. If value is String.Empty, the return value is startIndex. |
IndexOfAny(char[])
Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters.
Declaration
public int IndexOfAny(char[] anyOf)
Parameters
Type | Name | Description |
---|---|---|
char[] | anyOf | A Unicode character array containing one or more characters to seek. |
Returns
Type | Description |
---|---|
int | The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found. |
IndexOfAny(char[], int)
Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. The search starts at a specified character position.
Declaration
public int IndexOfAny(char[] anyOf, int startIndex)
Parameters
Type | Name | Description |
---|---|---|
char[] | anyOf | A Unicode character array containing one or more characters to seek. |
int | startIndex | The search starting position. |
Returns
Type | Description |
---|---|
int | The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found. |
IndexOfAny(char[], int, int)
Reports the zero-based index of the first occurrence in this instance of any character in a specified array of Unicode characters. The search starts at a specified character position and examines a specified number of character positions.
Declaration
public int IndexOfAny(char[] anyOf, int startIndex, int count)
Parameters
Type | Name | Description |
---|---|---|
char[] | anyOf | A Unicode character array containing one or more characters to seek. |
int | startIndex | The search starting position. |
int | count | The number of character positions to examine. |
Returns
Type | Description |
---|---|
int | The zero-based index position of the first occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found. |
Intern(string)
Retrieves the system's reference to the specified String.
Declaration
public static string Intern(string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | A string to search for in the intern pool. |
Returns
Type | Description |
---|---|
string | The system's reference to str, if it is interned; otherwise, a new reference to a string with the value of str. |
IsInterned(string)
Retrieves a reference to a specified String.
Declaration
public static string IsInterned(string str)
Parameters
Type | Name | Description |
---|---|---|
string | str | The string to search for in the intern pool. |
Returns
Type | Description |
---|---|
string | A reference to str if it is in the common language runtime intern pool; otherwise, null. |
IsNullOrEmpty(string)
Indicates whether the specified string is null or an empty string ("").
Declaration
public static bool IsNullOrEmpty(string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to test. |
Returns
Type | Description |
---|---|
bool | true if the value parameter is null or an empty string (""); otherwise, false. |
LastIndexOf(char)
Reports the zero-based index position of the last occurrence of a specified Unicode character within this instance.
Declaration
public int LastIndexOf(char value)
Parameters
Type | Name | Description |
---|---|---|
char | value | The Unicode character to seek. |
Returns
Type | Description |
---|---|
int | The zero-based index position of value if that character is found, or -1 if it is not. |
LastIndexOf(char, int)
Reports the zero-based index position of the last occurrence of a specified Unicode character within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string.
Declaration
public int LastIndexOf(char value, int startIndex)
Parameters
Type | Name | Description |
---|---|---|
char | value | The Unicode character to seek. |
int | startIndex | The starting position of the search. The search proceeds from startIndex toward the beginning of this instance. |
Returns
Type | Description |
---|---|
int | The zero-based index position of value if that character is found, or -1 if it is not found or if the current instance equals String.Empty. |
LastIndexOf(char, int, int)
Reports the zero-based index position of the last occurrence of the specified Unicode character in a substring within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string for a specified number of character positions.
Declaration
public int LastIndexOf(char value, int startIndex, int count)
Parameters
Type | Name | Description |
---|---|---|
char | value | The Unicode character to seek. |
int | startIndex | The starting position of the search. The search proceeds from startIndex toward the beginning of this instance. |
int | count | The number of character positions to examine. |
Returns
Type | Description |
---|---|
int | The zero-based index position of value if that character is found, or -1 if it is not found or if the current instance equals String.Empty. |
LastIndexOf(string)
Reports the zero-based index position of the last occurrence of a specified string within this instance.
Declaration
public int LastIndexOf(string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to seek. |
Returns
Type | Description |
---|---|
int | The zero-based starting index position of value if that string is found, or -1 if it is not. If value is String.Empty, the return value is the last index position in this instance. |
LastIndexOf(string, int)
Reports the zero-based index position of the last occurrence of a specified string within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string.
Declaration
public int LastIndexOf(string value, int startIndex)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to seek. |
int | startIndex | The search starting position. The search proceeds from startIndex toward the beginning of this instance. |
Returns
Type | Description |
---|---|
int | The zero-based starting index position of value if that string is found, or -1 if it is not found or if the current instance equals String.Empty. If value is String.Empty, the return value is the smaller of startIndex and the last index position in this instance. |
LastIndexOf(string, int, int)
Reports the zero-based index position of the last occurrence of a specified string within this instance. The search starts at a specified character position and proceeds backward toward the beginning of the string for a specified number of character positions.
Declaration
public int LastIndexOf(string value, int startIndex, int count)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to seek. |
int | startIndex | The search starting position. The search proceeds from startIndex toward the beginning of this instance. |
int | count | The number of character positions to examine. |
Returns
Type | Description |
---|---|
int | The zero-based starting index position of value if that string is found, or -1 if it is not found or if the current instance equals String.Empty. If value is Empty, the return value is the smaller of startIndex and the last index position in this instance. |
LastIndexOfAny(char[])
Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array.
Declaration
public int LastIndexOfAny(char[] anyOf)
Parameters
Type | Name | Description |
---|---|---|
char[] | anyOf | A Unicode character array containing one or more characters to seek. |
Returns
Type | Description |
---|---|
int | The index position of the last occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found. |
LastIndexOfAny(char[], int)
Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array. The search starts at a specified character position and proceeds backward toward the beginning of the string.
Declaration
public int LastIndexOfAny(char[] anyOf, int startIndex)
Parameters
Type | Name | Description |
---|---|---|
char[] | anyOf | A Unicode character array containing one or more characters to seek. |
int | startIndex | The search starting position. The search proceeds from startIndex toward the beginning of this instance. |
Returns
Type | Description |
---|---|
int | The index position of the last occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found or if the current instance equals String.Empty. |
LastIndexOfAny(char[], int, int)
Reports the zero-based index position of the last occurrence in this instance of one or more characters specified in a Unicode array. The search starts at a specified character position and proceeds backward toward the beginning of the string for a specified number of character positions.
Declaration
public int LastIndexOfAny(char[] anyOf, int startIndex, int count)
Parameters
Type | Name | Description |
---|---|---|
char[] | anyOf | A Unicode character array containing one or more characters to seek. |
int | startIndex | The search starting position. The search proceeds from startIndex toward the beginning of this instance. |
int | count | The number of character positions to examine. |
Returns
Type | Description |
---|---|
int | The index position of the last occurrence in this instance where any character in anyOf was found; -1 if no character in anyOf was found or if the current instance equals String.Empty. |
PadLeft(int, char)
Returns a new string that right-aligns the characters in this instance by padding them on the left with a specified Unicode character, for a specified total length.
Declaration
public string PadLeft(int totalWidth, char paddingChar = ' ')
Parameters
Type | Name | Description |
---|---|---|
int | totalWidth | The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters. |
char | paddingChar | A Unicode padding character. |
Returns
Type | Description |
---|---|
string |
PadRight(int, char)
Returns a new string that left-aligns the characters in this string by padding them on the right with a specified Unicode character, for a specified total length.
Declaration
public string PadRight(int totalWidth, char paddingChar = ' ')
Parameters
Type | Name | Description |
---|---|---|
int | totalWidth | The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters. |
char | paddingChar | A Unicode padding character. |
Returns
Type | Description |
---|---|
string |
Split(params char[])
Splits a string into substrings that are based on the characters in an array.
Declaration
public string[] Split(params char[] separator)
Parameters
Type | Name | Description |
---|---|---|
char[] | separator | A character array that delimits the substrings in this string, an empty array that contains no delimiters, or null. |
Returns
Type | Description |
---|---|
string[] | An array whose elements contain the substrings from this instance that are delimited by one or more characters in separator. For more information, see the Remarks section. |
Split(char[], int)
Splits a string into a maximum number of substrings based on the characters in an array. You also specify the maximum number of substrings to return.
Declaration
public string[] Split(char[] separator, int count)
Parameters
Type | Name | Description |
---|---|---|
char[] | separator | A character array that delimits the substrings in this string, an empty array that contains no delimiters, or null. |
int | count | The maximum number of substrings to return. |
Returns
Type | Description |
---|---|
string[] | An array whose elements contain the substrings in this instance that are delimited by one or more characters in separator. For more information, see the Remarks section. |
StartsWith(string)
Determines whether the beginning of this string instance matches the specified string.
Declaration
public bool StartsWith(string value)
Parameters
Type | Name | Description |
---|---|---|
string | value | The string to compare. |
Returns
Type | Description |
---|---|
bool | true if |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Substring(int)
Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
Declaration
public string Substring(int startIndex)
Parameters
Type | Name | Description |
---|---|---|
int | startIndex | The zero-based starting character position of a substring in this instance. |
Returns
Type | Description |
---|---|
string | A string that is equivalent to the substring that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance. |
Substring(int, int)
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
Declaration
public string Substring(int startIndex, int length)
Parameters
Type | Name | Description |
---|---|---|
int | startIndex | The zero-based starting character position of a substring in this instance. |
int | length | The number of characters in the substring. |
Returns
Type | Description |
---|---|
string | A string that is equivalent to the substring of length length that begins at startIndex in this instance, or Empty if startIndex is equal to the length of this instance and length is zero. |
ToCharArray()
Copies the characters in this instance to a Unicode character array.
Declaration
public char[] ToCharArray()
Returns
Type | Description |
---|---|
char[] | A Unicode character array whose elements are the individual characters of this instance. If this instance is an empty string, the returned array is empty and has a zero length. |
ToCharArray(int, int)
Copies the characters in a specified substring in this instance to a Unicode character array.
Declaration
public char[] ToCharArray(int startIndex, int length)
Parameters
Type | Name | Description |
---|---|---|
int | startIndex | The starting position of a substring in this instance. |
int | length | The length of the substring in this instance. |
Returns
Type | Description |
---|---|
char[] | A Unicode character array whose elements are the length number of characters in this instance starting from character position startIndex. |
ToLower()
Returns a copy of this string converted to lowercase.
Declaration
public string ToLower()
Returns
Type | Description |
---|---|
string | A string in lowercase. |
ToString()
Returns this instance of String; no actual conversion is performed.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | The current string. |
Overrides
ToUpper()
Returns a copy of this string converted to uppercase.
Declaration
public string ToUpper()
Returns
Type | Description |
---|---|
string | The uppercase equivalent of the current string. |
Trim()
Removes all leading and trailing white-space characters from the current String object.
Declaration
public string Trim()
Returns
Type | Description |
---|---|
string | The string that remains after all white-space characters are removed from the start and end of the current string. If no characters can be trimmed from the current instance, the method returns the current instance unchanged. |
Trim(params char[])
Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.
Declaration
public string Trim(params char[] trimChars)
Parameters
Type | Name | Description |
---|---|---|
char[] | trimChars | An array of Unicode characters to remove, or null. |
Returns
Type | Description |
---|---|
string | The string that remains after all occurrences of the characters in the trimChars parameter are removed from the start and end of the current string. If trimChars is null or an empty array, white-space characters are removed instead. If no characters can be trimmed from the current instance, the method returns the current instance unchanged. |
TrimEnd(params char[])
Removes all trailing occurrences of a set of characters specified in an array from the current String object.
Declaration
public string TrimEnd(params char[] trimChars)
Parameters
Type | Name | Description |
---|---|---|
char[] | trimChars | An array of Unicode characters to remove, or null. |
Returns
Type | Description |
---|---|
string | The string that remains after all occurrences of the characters in the trimChars parameter are removed from the end of the current string. If trimChars is null or an empty array, Unicode white-space characters are removed instead. If no characters can be trimmed from the current instance, the method returns the current instance unchanged. |
TrimStart(params char[])
Removes all leading occurrences of a set of characters specified in an array from the current String object.
Declaration
public string TrimStart(params char[] trimChars)
Parameters
Type | Name | Description |
---|---|---|
char[] | trimChars | An array of Unicode characters to remove, or null. |
Returns
Type | Description |
---|---|
string | The string that remains after all occurrences of characters in the trimChars parameter are removed from the start of the current string. If trimChars is null or an empty array, white-space characters are removed instead. |
Operators
operator ==(string, string)
Determines whether two specified strings have the same value.
Declaration
public static bool operator ==(string a, string b)
Parameters
Type | Name | Description |
---|---|---|
string | a | The first string to compare, or null. |
string | b | The second string to compare, or null. |
Returns
Type | Description |
---|---|
bool | true if the value of a is the same as the value of b; otherwise, false. |
operator !=(string, string)
Determines whether two specified strings have different values.
Declaration
public static bool operator !=(string a, string b)
Parameters
Type | Name | Description |
---|---|---|
string | a | The first string to compare, or null. |
string | b | The second string to compare, or null. |
Returns
Type | Description |
---|---|
bool | true if the value of a is different from the value of b; otherwise, false. |