System.String Class

Namespace: System

Represents text as a sequence of characters. Strings are immutable, meaning that once a string object is created, its value cannot be changed.

Summary

The String class is fundamental to .NET development, providing rich functionality for manipulating text. Because strings are immutable, operations that appear to modify a string actually create a new string object with the modified content.

Remarks

The String class provides a vast array of static and instance methods for common string operations such as:

The String class implements the ICloneable and IComparable interfaces, and it supports the standard comparison operators.

In C#, string literals are enclosed in double quotes (e.g., "Hello, World!"). Verbatim string literals, which treat backslashes literally, are prefixed with @ (e.g., @"C:\MyFolder\MyFile.txt").

Fields

Name Description
Empty Represents an empty string. This field is read-only.

Constructors

Name Description
String(Char[ ]) Initializes a new instance of the String class by using the specified array of characters.
String(Char[ ], Int32, Int32) Initializes a new instance of the String class by using a subarray of characters, starting at the specified index, and occupying the specified number of characters.

Methods

Name Description
string.IsNullOrEmpty(string value) Indicates whether the specified string is null or an Empty string.
string.IsNullOrWhiteSpace(string value) Indicates whether the specified string is null, empty, or consists only of white-space characters.
string.Compare(string strA, string strB) Compares two strings lexicographically, taking into account the current culture and ignoring case.
string.Concat(params object[ ] args) Concatenates the string representations of the elements of an object array, followed by the elements of a string array, into a single string.
string.Format(string format, params object[ ] arg) Formats the value of the current instance or any other provided value into the representation specified by the format string.
string.Replace(char oldValue, char newValue) Replaces all occurrences of a specified character in this instance with another specified character.
string.Split(char separator) Splits a string into substrings by separating them at each character that is the same as the specified separator.
string.Substring(int startIndex) Retrieves a substring from the current string. The substring starts at a specified character position and continues to the end of the string.
string.Trim() Removes all leading and trailing white space characters from the current String object.

Properties

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

Examples

String Concatenation


string firstName = "John";
string lastName = "Doe";
string fullName = string.Concat(firstName, " ", lastName);
Console.WriteLine(fullName); // Output: John Doe

string message = $"Hello, {firstName}!";
Console.WriteLine(message); // Output: Hello, John!
            

Checking for Null or Empty


string nullString = null;
string emptyString = "";
string nonEmptyString = "Some text";

if (string.IsNullOrEmpty(nullString))
{
    Console.WriteLine("nullString is null or empty.");
}

if (!string.IsNullOrWhiteSpace(nonEmptyString))
{
    Console.WriteLine("nonEmptyString has content.");
}
            

String Formatting


string productName = "Laptop";
decimal price = 1200.50m;
string formattedString = string.Format("Product: {0}, Price: {1:C}", productName, price);
Console.WriteLine(formattedString); // Output: Product: Laptop, Price: $1,200.50 (culture dependent)
            

Substring Extraction


string fullText = "This is a sample sentence.";
string sub = fullText.Substring(10); // Starts at index 10
Console.WriteLine(sub); // Output: sample sentence.

string partial = fullText.Substring(0, 4); // From index 0, length 4
Console.WriteLine(partial); // Output: This
            

See Also