Microsoft Learn

String Class

// Represents an immutable sequence of characters.

The System.String class represents a text string. A string is a sequence of Unicode characters. Strings are ordered sequences of characters of a specified length. The value of a string is.

Summary

Constructor Description
String(Char[]) Initializes a new instance of the String class by using an array of 16-bit Unicode characters.
String(Char[], Int32, Int32) Initializes a new instance of the String class by using a subarray of 16-bit Unicode characters, a starting position, and a length.
Method Description
IsNullOrEmpty(String) Indicates whether the specified string is null or an Empty string.
IsNullOrWhiteSpace(String) Indicates whether the specified string is null, empty, or consists only of white-space characters.
Compare(String, String) Compares two strings using the conventions of the current culture.
Concat(Object[]) Concatenates the string representations of the elements in a specified array of objects.
EndsWith(String) Determines whether the end of this string instance matches the specified string.
IndexOf(String) Reports the zero-based index of the first occurrence of a specified string within this instance.
Insert(Int32, String) Inserts a specified string into this instance at a specified character position.
Join(String, String[]) Concatenates the elements of a string array, using the specified separator between each element.
Remove(Int32) Removes a specified number of characters from the current string beginning at a specified position.
Replace(Char, Char) Replaces all occurrences of a specified Unicode character in this instance with another specified Unicode character.
Split(Char[]) Splits a string into substrings based on specified delimiters.
StartsWith(String) Determines whether the beginning of this string instance matches the specified string.
Substring(Int32) Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.
ToLower() Converts this string to lowercase.
ToUpper() Converts this string to uppercase.
Trim() Removes all leading and trailing white space characters from the current String object.

Remarks

Strings are immutable objects. This means that once a string object is created, its value cannot be changed. Methods that appear to modify a string, such as Replace or Substring, actually return a new string object whose value is the result of the operation.

Immutability Example

using System;

public class Example
{
    public static void Main()
    {
        string original = "Hello";
        string modified = original.Replace('l', 'x');

        Console.WriteLine($"Original: {original}"); // Output: Original: Hello
        Console.WriteLine($"Modified: {modified}"); // Output: Modified: Hexxo
    }
}

Related Topics