String Class in C#

Last Updated : 22 Oct, 2026

The String class is used to represent and work with sequences of characters. It belongs to the System namespace and provides methods for creating, comparing, searching, and manipulating strings. String objects are immutable, meaning their values cannot be changed after creation.

  • String literals can be stored and reused through string interning.
  • Strings can be created using string literals or the String class constructors.

Key Features of the String Class

1. Immutable

Immutable means that once a string object is created, its value cannot be changed. Any operation that modifies a string returns a new string instead of changing the original one.

  • String characters cannot be modified directly using an index.
  • Methods such as ToUpper() and Replace() return a new string.

Example: Program to demonstrate the immutability of strings.

C#
using System;
class Geeks {
    static void Main()
    {
        string text = "hello"; 
        
        // Creates a new string
        text = text.ToUpper();
        Console.WriteLine(text);
    }
}

Output
HELLO

Syntax:

string variableName = "value";

2. Thread-Safe

String objects are safe to share between threads for reading because they are immutable. Once created, their character contents cannot be changed.

  • Multiple threads can read the same string without modifying its contents.
  • Operations that change string content return a new string rather than modifying the existing one.

Example: Program to demonstrate that assigning a modified string does not change the original value.

C#
using System;

class Geeks{
    
    static void Main(){
        string str1 = "Hello";
        string str2 = str1;
        
        str2 = str2 + " C#";
        
        Console.WriteLine("str1: " + str1);
        Console.WriteLine("str2: " + str2);
    }
}

Output
str1: Hello
str2: Hello C#

Syntax:

string str1 = "Hello";
string str2 = str1;

3. Supports Various Utility Methods

The String class is a predefined class in the System namespace. It provides methods and properties to create, manipulate, search, and compare strings, such as Length, ToUpper(), ToLower(), Contains(), and Replace().

  • Length returns the number of characters in a string.
  • Methods such as ToUpper() and Replace() return a new string with the requested changes.

Example: Program to demonstrate commonly used string methods.

C#
using System;

class Geeks{
    static void Main(){
        string str = "hello geeks";

        Console.WriteLine("Length of String -> " + str.Length);
        Console.WriteLine("Changed String -> " + str.ToUpper());
    }
}

Output
Length of String -> 11
Changed String -> HELLO GEEKS

Syntax:

int length = str.Length;
string upper = str.ToUpper();

4. Implements Interfaces

The String class implements interfaces that provide functionality for character sequences, comparisons, and equality checks.

  • IComparable<string>: Enables string comparison using CompareTo().
  • IEnumerable<char>: Allows the characters in a string to be enumerated.
  • IEquatable<string>: Supports equality comparisons using Equals().

String Constructors

The Strings are usually created using string literals. The String class also provides constructors for creating strings from character arrays or repeated characters.

  • Constructors allow strings to be created from different character-based sources.
  • The new keyword can explicitly create a string object, although string literals are generally preferred for ordinary string initialization.

Example: Program demonstrates different ways to create strings using constructors.

C#
using System;

class Geeks{
    static void Main(){
        
        // Constructor 1: Creating a string using new
        string str1 = new string("Hello C#".ToCharArray());
        Console.WriteLine("String using constructor: " + str1);

        // Constructor 2: Creating a string from a character array
        char[] charArray = { 'C', '#', ' ', 'C', 'o', 'd', 'e' };
        string str2 = new string(charArray);
        Console.WriteLine("String from char array: " + str2);

        // Constructor 3: Creating a string using a repeated character 
        string str3 = new string('*', 5);
        Console.WriteLine("Repeated characters: " + str3);
    }
}

Output
String using constructor: Hello C#
String from char array: C# Code
Repeated characters: *****

Syntax:

string str1 = new string(charArray);
string str2 = new string(charArray, startIndex, length);
string str3 = new string('*', 5);

String Constructors

The String class provides the following commonly used constructors:

  • String(char[] value): Creates a string from a character array.
  • String(char[] value, int startIndex, int length): Creates a string from a specified range of a character array.
  • String(char c, int count): Creates a string containing a specified character repeated a given number of times.tes a string containing a specified character repeated a given number of times.

1. String(char[] value)

Creates a string from the characters in the specified array.

Example: The following code creates a string from a character array.

C#
char[] chars = { 'C', '#', ' ' };
string str = new string(chars);

Console.WriteLine(str);

Output

C# 

Syntax:

string str = new string(charArray);

2. String(char[] value, int startIndex, int length)

Creates a string using a specified number of characters from a character array, starting at the given index.

Example: The following code creates a string from a selected range of a character array.

C#
char[] chars = { 'G', 'e', 'e', 'k', 's' };
string str = new string(chars, 1, 3);

Console.WriteLine(str);

Output

eek

Syntax:

string str = new string(charArray, startIndex, length);

3. String(char c, int count)

Creates a string containing the specified character repeated a given number of times.

Example: The following code creates a string containing five asterisks.

C#
using System;
class Geeks {
    static void Main()
    {
        string str = new string('*', 5);
        Console.WriteLine(str);
    }
}

Output
*****

Syntax:

string str = new string(character, count);

Methods of String Class

Method

Description

ToUpper() 

Converts all characters to uppercase.

ToLower() 

Converts all characters to lowercase.

Trim() 

Removes whitespace from the beginning and end of a string.

TrimStart() 

Removes whitespace or specified characters from the beginning.

TrimEnd() 

Removes whitespace or specified characters from the end.

Substring() 

Extracts a substring from a specified position.

Replace() 

Replaces occurrences of a specified character or substring.

Remove() 

Removes characters from a specified position.

Insert() 

Inserts a string at a specified index.

Concat() 

Combines two or more strings.

Join() 

Combines strings using a specified separator.

Split() 

Splits a string into an array of substrings.

Contains() 

Checks whether a string contains a specified value.

StartsWith() 

Checks whether a string starts with a specified value.

EndsWith() 

Checks whether a string ends with a specified value.

IndexOf() 

Returns the index of the first occurrence of a specified value.

LastIndexOf() 

Returns the index of the last occurrence of a specified value.

IndexOfAny() 

Finds the first occurrence of any specified character.

LastIndexOfAny() 

Finds the last occurrence of any specified character.

Compare() 

Compares two strings and indicates their sort order.

CompareTo() 

Compares the current string with another string.

CompareOrdinal() 

Compares two strings using ordinal comparison.

Equals() 

Determines whether two strings are equal.

IsNullOrEmpty() 

Checks whether a string is null or empty.

IsNullOrWhiteSpace() 

Checks whether a string is null, empty, or contains only whitespace.

Format() 

Creates a formatted string using placeholders.

ToCharArray() 

Converts a string into a character array.

PadLeft() 

Pads the beginning of a string to a specified total length.

PadRight() 

Pads the end of a string to a specified total length.

ToUpperInvariant() 

Converts a string to uppercase using invariant culture rules.

ToLowerInvariant() 

Converts a string to lowercase using invariant culture rules.

Normalize() 

Returns a Unicode-normalized string.

IsNormalized() 

Checks whether a string is in a specified Unicode normalization form.

CopyTo() 

Copies characters into a character array.

TryCopyTo() 

Attempts to copy the string into a destination character span.

GetEnumerator() 

Returns an enumerator for iterating through the string's characters.

ToString() 

Returns the current string instance.

GetHashCode() 

Returns a hash code for the string.

Clone() 

Returns a reference to the current string instance.

Comment

Explore