String Class

Namespace: System
Assembly: System.Runtime

Represents text as a sequence of zero or more 16-bit Unicode characters. The String class is an immutable type, which means that an object cannot be modified after it is created.

Properties

Name Description
Length Gets the number of characters in the current String object.

Methods

Name Description
Compare Compares two strings and returns an integer indicating whether the first string precedes, follows, or coincides with the second string in sort order.
Concat Concatenates the specified number of string values.
Contains Determines whether the specified substring occurs within this string.
EndsWith Determines whether the specified string occurs at the end of this string.
Equals Determines whether two string instances are equal.
Format Replaces the format item in a specified string or a span of characters to their string representation using specified format information.
IndexOf Returns the zero-based index of the first occurrence of the specified Unicode character within this instance.
IsNullOrEmpty Indicates whether the specified string is null or an empty string (that is, contains no characters).
IsNullOrWhiteSpace Indicates whether the specified string is null, empty, or consists only of white-space characters.
Join Concatenates the elements of a collection, using the specified separator between each element.
LastIndexOf Returns the zero-based index of the last occurrence of the specified Unicode character within this instance.
Remove Returns a new string in which a specified number of characters in the current instance are removed, and optionally, a new character is inserted in that position.
Replace Returns a new string in which all occurrences of a specified character or string in this instance are replaced with another specified character or string.
Split Splits a string into substrings based on specified delimiters.
StartsWith Determines whether the beginning of this string instance matches the specified string.
Substring Retrieves a substring from this instance. The substring retrieves the characters in a specified position and with a specified length.
ToLower Converts this string to its equivalent lowercase Unicode character representation.
ToUpper Converts this string to its equivalent uppercase Unicode character representation.
Trim Removes all leading and trailing white space characters from the current String object.

Remarks

Because String objects are immutable, methods that appear to modify a string actually return a new string object that contains the modified information. For example, the Replace method does not change the value of the original string object; instead, it returns a new string object whose value is the result of the replacement operation.

For efficient string manipulation, especially when performing many operations that modify strings, consider using the StringBuilder class. The StringBuilder class is a mutable type that allows you to modify its contents without creating new objects.

Examples

Creating and Manipulating Strings

string greeting = "Hello";
string name = "World";
string message = String.Concat(greeting, ", ", name, "!"); // "Hello, World!"

Console.WriteLine(message);
Console.WriteLine($"Length of message: {message.Length}"); // Length of message: 13

if (message.Contains("World"))
{
    Console.WriteLine("The message contains 'World'.");
}

string upperMessage = message.ToUpper(); // "HELLO, WORLD!"
Console.WriteLine(upperMessage);

string replacedMessage = message.Replace("World", "Universe"); // "Hello, Universe!"
Console.WriteLine(replacedMessage);

Checking String Properties

string emptyString = "";
string nullString = null;
string whiteSpaceString = "   ";

Console.WriteLine($"Is emptyString null or empty? {String.IsNullOrEmpty(emptyString)}"); // True
Console.WriteLine($"Is nullString null or empty? {String.IsNullOrEmpty(nullString)}");   // True
Console.WriteLine($"Is whiteSpaceString null or empty? {String.IsNullOrEmpty(whiteSpaceString)}"); // False

Console.WriteLine($"Is whiteSpaceString null or white space? {String.IsNullOrWhiteSpace(whiteSpaceString)}"); // True