.NET Core API Documentation

Microsoft Learn

System.String Class

Represents a text string. String objects are immutable, meaning their value cannot be changed after creation. Any operation that appears to modify a string actually creates a new string object.

Methods

Compare(string strA, string strB)

static int Compare(string strA, string strB)

Compares two strings, returning a value indicating whether one is less than, equal to, or greater than the other.

Parameters:
strA: The first string to compare.
strB: The second string to compare.
Returns:
A signed integer that indicates the lexical relationship between the two strings.
  • Less than zero: strA precedes strB.
  • Zero: strA and strB are equal.
  • Greater than zero: strA follows strB.

CompareTo(string value)

int CompareTo(string value)

Compares the current string instance to another string.

Parameters:
value: The string to compare with the current string.
Returns:
A signed integer that indicates the lexical relationship between the current string and the value parameter.

Contains(char value)

bool Contains(char value)

Determines whether the string contains a specified character.

Parameters:
value: The character to search for.
Returns:
true if the string contains the specified character; otherwise, false.

EndsWith(string value)

bool EndsWith(string value)

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

Parameters:
value: The string to compare with the end of this string.
Returns:
true if the character sequence represented by value corresponds to the characters at the end of this string; otherwise, false.

Equals(string value)

bool Equals(string value)

Determines whether two strings are equal.

Parameters:
value: The string to compare to the current string.
Returns:
true if the current string and value represent the same sequence of characters; otherwise, false.

IndexOf(char value)

int IndexOf(char value)

Reports the zero-based index of the first occurrence of a specified Unicode character within this instance.

Parameters:
value: The Unicode character to seek.
Returns:
The zero-based index of the first occurrence of value if found; otherwise, -1.

IsNullOrEmpty(string value)

static bool IsNullOrEmpty(string value)

Indicates whether the specified string is null or an empty string (that is, it contains no characters).

Parameters:
value: The string to test.
Returns:
true if the value parameter is null or an empty string; otherwise, false.

Join(string separator, params string[] values)

static string Join(string separator, params string[] values)

Concatenates the elements of a string array, using the specified separator between each element.

Parameters:
separator: The string to use as a separator. separator is included in each element of the returned string, except for the first element.
values: An array of strings to concatenate.
Returns:
A string that consists of the elements of values, separated by separator.

Replace(char oldChar, char newChar)

string Replace(char oldChar, char newChar)

Replaces all occurrences of a specified Unicode character in this instance with another specified Unicode character.

Parameters:
oldChar: The character to be replaced.
newChar: The character that replaces oldChar.
Returns:
A new string instance in which all occurrences of oldChar are replaced with newChar.

Split(params char[] separator)

string[] Split(params char[] separator)

Splits this string into substrings by separating it into substrings by the elements of a specified character array.

Parameters:
separator: A character array that contains one or more characters to use as separators. If separator is null or an empty array, the string is split at each character.
Returns:
A string array that contains the substrings from this instance, delimited by the characters in separator. If separator is null or an empty array, the substrings are delimited by each character of the string.

StartsWith(string value)

bool StartsWith(string value)

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

Parameters:
value: The string to compare with the beginning of this string.
Returns:
true if the characters of value match the beginning of this string; otherwise, false.

Substring(int startIndex)

string Substring(int startIndex)

Extracts a substring from the current string. The substring starts at a specified character position and continues to the end of the string.

Parameters:
startIndex: The zero-based starting character position of the substring.
Returns:
A string that is equivalent to the substring of the current instance that begins at startIndex and ends at the last character.

ToLower()

string ToLower()

Converts this string to lowercase.

Returns:
A string in lowercase.

ToUpper()

string ToUpper()

Converts this string to uppercase.

Returns:
A string in uppercase.

Trim()

string Trim()

Removes all white space characters from the current string.

Returns:
A string that has no white space characters from the beginning or end of the current string. If there are no white space characters in the current string, the method returns a reference to the current string.

TrimStart(char trimChar)

string TrimStart(char trimChar)

Removes all leading occurrences of a specified character from the current string.

Parameters:
trimChar: The Unicode character to remove.
Returns:
A string that remains after all occurrences of trimChar specified at the beginning of the current string are removed. If all characters in the current string match the trimChar parameter, or if the string is empty, the method returns a null reference.