Table of Contents

Class string

Namespace
System
Assembly
mscorlib.dll

Represents text as a sequence of UTF-16 code units.

public sealed string : IComparable, IEnumerable
Inheritance
string
Implements
Inherited Members
Extension Methods

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.

public String(char c, int count)

Parameters

c char

A Unicode character.

count int

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.

public String(char[] value)

Parameters

value char[]

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.

public String(char[] value, int startIndex, int length)

Parameters

value char[]

An array of Unicode characters.

startIndex int

The starting position within value.

length int

The number of characters within value to use.

Fields

Empty

Represents the empty string. This field is read-only.

public static readonly string Empty

Field Value

string

Properties

this[int]

Gets the Char object at a specified position in the current String object.

public char this[int index] { get; }

Parameters

index int

A position in the current string.

Property Value

char

The object at position index.

Length

Gets the number of characters in the current String object.

public int Length { get; }

Property Value

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.

public static int Compare(string strA, string strB)

Parameters

strA string

The first string to compare.

strB string

The second string to compare.

Returns

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.

public int CompareTo(object value)

Parameters

value object

An object that evaluates to a String.

Returns

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.

public int CompareTo(string strB)

Parameters

strB string

The string to compare with this instance.

Returns

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.

public static string Concat(object arg0)

Parameters

arg0 object

The object to represent, or null.

Returns

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.

public static string Concat(object arg0, object arg1)

Parameters

arg0 object

The first object to concatenate.

arg1 object

The second object to concatenate.

Returns

string

The concatenated string representations of the values of arg0 and arg1.

Concat(object, object, object)

Concatenates the string representations of three specified objects.

public static string Concat(object arg0, object arg1, object arg2)

Parameters

arg0 object

The first object to concatenate.

arg1 object

The second object to concatenate.

arg2 object

The third object to concatenate.

Returns

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.

public static string Concat(params object[] args)

Parameters

args object[]

An object array that contains the elements to concatenate.

Returns

string

The concatenated string representations of the values of the elements in args.

Exceptions

ArgumentNullException

Concat(string, string)

Concatenates two specified instances of String.

public static string Concat(string str0, string str1)

Parameters

str0 string

The first string to concatenate.

str1 string

The second string to concatenate.

Returns

string

The concatenation of str0 and str1.

Concat(string, string, string)

Concatenates three specified instances of String.

public static string Concat(string str0, string str1, string str2)

Parameters

str0 string

The first string to concatenate.

str1 string

The second string to concatenate.

str2 string

The third string to concatenate.

Returns

string

The concatenation of str0, str1 and str2.

Concat(string, string, string, string)

Concatenates four specified instances of String.

public static string Concat(string str0, string str1, string str2, string str3)

Parameters

str0 string

The first string to concatenate.

str1 string

The second string to concatenate.

str2 string

The third string to concatenate.

str3 string

The fourth string to concatenate.

Returns

string

The concatenation of str0, str1, str2 and str3.

Concat(params string[])

Concatenates the elements of a specified String array.

public static string Concat(params string[] values)

Parameters

values string[]

An array of string instances.

Returns

string

The concatenated elements of values.

Contains(string)

Returns a value indicating whether a specified substring occurs within this string.

public bool Contains(string value)

Parameters

value string

The string to seek.

Returns

bool

true if the value parameter occurs within this string, or if value is the empty string (""); otherwise, false.

EndsWith(string)

Determines whether the end of this string instance matches the specified string.

public bool EndsWith(string value)

Parameters

value string

The string to compare to the substring at the end of this instance.

Returns

bool

true if value matches the end of this instance; otherwise, false.

Equals(object)

Determines whether this instance and a specified object, which must also be a String object, have the same value.

public override bool Equals(object obj)

Parameters

obj object

The string to compare to this instance.

Returns

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.

Equals(string, string)

Determines whether two specified String objects have the same value.

public static bool Equals(string a, string b)

Parameters

a string

The first string to compare, or null.

b string

The second string to compare, or null.

Returns

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.

public static string Format(string format, params object[] args)

Parameters

format string

A composite format string

args object[]

An object array that contains zero or more objects to format.

Returns

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.

public IEnumerator GetEnumerator()

Returns

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.

public int IndexOf(char value)

Parameters

value char

A Unicode character to seek.

Returns

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.

public int IndexOf(char value, int startIndex)

Parameters

value char

A Unicode character to seek.

startIndex int

The search starting position.

Returns

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.

public int IndexOf(char value, int startIndex, int count)

Parameters

value char

A Unicode character to seek.

startIndex int

The search starting position.

count int

The number of character positions to examine.

Returns

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.

public int IndexOf(string value)

Parameters

value string

The string to seek.

Returns

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.

public int IndexOf(string value, int startIndex)

Parameters

value string

The string to seek.

startIndex int

The search starting position.

Returns

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.

public int IndexOf(string value, int startIndex, int count)

Parameters

value string

The string to seek.

startIndex int

The search starting position.

count int

The number of character positions to examine.

Returns

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.

public int IndexOfAny(char[] anyOf)

Parameters

anyOf char[]

A Unicode character array containing one or more characters to seek.

Returns

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.

public int IndexOfAny(char[] anyOf, int startIndex)

Parameters

anyOf char[]

A Unicode character array containing one or more characters to seek.

startIndex int

The search starting position.

Returns

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.

public int IndexOfAny(char[] anyOf, int startIndex, int count)

Parameters

anyOf char[]

A Unicode character array containing one or more characters to seek.

startIndex int

The search starting position.

count int

The number of character positions to examine.

Returns

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.

public static string Intern(string str)

Parameters

str string

A string to search for in the intern pool.

Returns

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.

public static string IsInterned(string str)

Parameters

str string

The string to search for in the intern pool.

Returns

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

public static bool IsNullOrEmpty(string value)

Parameters

value string

The string to test.

Returns

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.

public int LastIndexOf(char value)

Parameters

value char

The Unicode character to seek.

Returns

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.

public int LastIndexOf(char value, int startIndex)

Parameters

value char

The Unicode character to seek.

startIndex int

The starting position of the search. The search proceeds from startIndex toward the beginning of this instance.

Returns

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.

public int LastIndexOf(char value, int startIndex, int count)

Parameters

value char

The Unicode character to seek.

startIndex int

The starting position of the search. The search proceeds from startIndex toward the beginning of this instance.

count int

The number of character positions to examine.

Returns

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.

public int LastIndexOf(string value)

Parameters

value string

The string to seek.

Returns

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.

public int LastIndexOf(string value, int startIndex)

Parameters

value string

The string to seek.

startIndex int

The search starting position. The search proceeds from startIndex toward the beginning of this instance.

Returns

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.

public int LastIndexOf(string value, int startIndex, int count)

Parameters

value string

The string to seek.

startIndex int

The search starting position. The search proceeds from startIndex toward the beginning of this instance.

count int

The number of character positions to examine.

Returns

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.

public int LastIndexOfAny(char[] anyOf)

Parameters

anyOf char[]

A Unicode character array containing one or more characters to seek.

Returns

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.

public int LastIndexOfAny(char[] anyOf, int startIndex)

Parameters

anyOf char[]

A Unicode character array containing one or more characters to seek.

startIndex int

The search starting position. The search proceeds from startIndex toward the beginning of this instance.

Returns

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.

public int LastIndexOfAny(char[] anyOf, int startIndex, int count)

Parameters

anyOf char[]

A Unicode character array containing one or more characters to seek.

startIndex int

The search starting position. The search proceeds from startIndex toward the beginning of this instance.

count int

The number of character positions to examine.

Returns

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.

public string PadLeft(int totalWidth, char paddingChar = ' ')

Parameters

totalWidth int

The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.

paddingChar char

A Unicode padding character.

Returns

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.

public string PadRight(int totalWidth, char paddingChar = ' ')

Parameters

totalWidth int

The number of characters in the resulting string, equal to the number of original characters plus any additional padding characters.

paddingChar char

A Unicode padding character.

Returns

string

Split(params char[])

Splits a string into substrings that are based on the characters in an array.

public string[] Split(params char[] separator)

Parameters

separator char[]

A character array that delimits the substrings in this string, an empty array that contains no delimiters, or null.

Returns

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.

public string[] Split(char[] separator, int count)

Parameters

separator char[]

A character array that delimits the substrings in this string, an empty array that contains no delimiters, or null.

count int

The maximum number of substrings to return.

Returns

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.

public bool StartsWith(string value)

Parameters

value string

The string to compare.

Returns

bool

true if value matches the beginning of this string; otherwise, false.

Exceptions

ArgumentNullException

value is null.

Substring(int)

Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.

public string Substring(int startIndex)

Parameters

startIndex int

The zero-based starting character position of a substring in this instance.

Returns

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.

public string Substring(int startIndex, int length)

Parameters

startIndex int

The zero-based starting character position of a substring in this instance.

length int

The number of characters in the substring.

Returns

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.

public char[] ToCharArray()

Returns

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.

public char[] ToCharArray(int startIndex, int length)

Parameters

startIndex int

The starting position of a substring in this instance.

length int

The length of the substring in this instance.

Returns

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.

public string ToLower()

Returns

string

A string in lowercase.

ToString()

Returns this instance of String; no actual conversion is performed.

public override string ToString()

Returns

string

The current string.

ToUpper()

Returns a copy of this string converted to uppercase.

public string ToUpper()

Returns

string

The uppercase equivalent of the current string.

Trim()

Removes all leading and trailing white-space characters from the current String object.

public string Trim()

Returns

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.

public string Trim(params char[] trimChars)

Parameters

trimChars char[]

An array of Unicode characters to remove, or null.

Returns

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.

public string TrimEnd(params char[] trimChars)

Parameters

trimChars char[]

An array of Unicode characters to remove, or null.

Returns

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.

public string TrimStart(params char[] trimChars)

Parameters

trimChars char[]

An array of Unicode characters to remove, or null.

Returns

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.

get_Chars(int)

public char get_Chars(int index)

Parameters

index int

Returns

char

Operators

operator ==(string, string)

Determines whether two specified strings have the same value.

public static bool operator ==(string a, string b)

Parameters

a string

The first string to compare, or null.

b string

The second string to compare, or null.

Returns

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.

public static bool operator !=(string a, string b)

Parameters

a string

The first string to compare, or null.

b string

The second string to compare, or null.

Returns

bool

true if the value of a is different from the value of b; otherwise, false.