C# String Substring() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, Substring() copies part of a string into a new string. You give the start index, and optionally the length (how many characters to take). Without a length, it takes everything to the end.

The second argument is a length, not an end position. This is the most common beginner mistake: "Hello".Substring(1, 3) is "ell" (3 characters starting at index 1), not “from 1 to 3”. The original string is never changed.

This is an instance member of System.String. Call it on a string variable, such as text.Substring(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: Substring() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable.

Syntax

Pass the start index, and optionally how many characters to take.

C#
string text = "Hello, World";

string tail = text.Substring(7);       // "World"
string head = text.Substring(0, 5);    // "Hello"
string mid  = text.Substring(7, 3);    // "Wor"
CallWhat it does
text.Substring(7)From index 7 to the end.
text.Substring(0, 5)The first 5 characters.
text.Substring(i + 1)Everything after a character you found at i.
text[7..10]C# 8 range syntax: from index 7 up to (not including) 10. Same as Substring(7, 3).

startIndex may be 0 to Length; Substring(Length) returns an empty string. startIndex + length must not be larger than Length. Breaking either rule throws ArgumentOutOfRangeException. It never pads or shortens for you.

Library declarations
C#
public string Substring(int startIndex);
public string Substring(int startIndex, int length);

Parameters

ParameterDescription
startIndexRequired. The zero-based position of the first character to copy.
lengthOptional. How many characters to copy. Leave it out to copy to the end.

Return Value

Return ValueDescription
stringA new string with the requested characters. It can be "" when the length is 0.

C# Examples

Example 1 — Take the Start, the End, and the Middle

Slice a greeting three ways and compare the results.

C#
using System;

class Program {
    static void Main() {
        string text = "Hello, World";

        Console.WriteLine(text.Substring(7));
        Console.WriteLine(text.Substring(0, 5));
        Console.WriteLine(text.Substring(7, 3));
    }
}

How It Works

  1. Find the start. Hello is 0 to 4, the comma is 5, the space is 6, so W is at index 7.
  2. To the end. With one argument, Substring(7) copies World.
  3. A fixed count. Substring(0, 5) takes five characters from the front.
  4. Count, not end. Substring(7, 3) takes three characters, W, o, r, giving Wor.

Example 2 — Split a key=value Pair

Use IndexOf to find the equals sign and Substring to copy both sides.

C#
using System;

class Program {
    static void Main() {
        string pair = "name=Sam";
        int eq = pair.IndexOf('=');

        string key = pair.Substring(0, eq);
        string value = pair.Substring(eq + 1);

        Console.WriteLine(key);
        Console.WriteLine(value);
    }
}

How It Works

  1. Locate the divider. name takes 0 to 3, so the = is at index 4.
  2. Left side. Substring(0, eq) takes 4 characters. Because indexes start at 0, the divider's index is also the length of the part before it.
  3. Right side. Substring(eq + 1) skips the = and copies the rest, Sam.
  4. Real input needs a guard. If a line has no =, eq is -1 and Substring(0, -1) throws. Check first.

Example 3 — Avoid ArgumentOutOfRangeException

Ask for too many characters, then compute a safe length, and see the empty result at the very end.

C#
using System;

class Program {
    static void Main() {
        string s = "C#";

        try {
            Console.WriteLine(s.Substring(1, 5));
        } catch (ArgumentOutOfRangeException ex) {
            Console.WriteLine(ex.GetType().Name);
        }

        int len = Math.Min(5, s.Length - 1);
        Console.WriteLine(s.Substring(1, len));
        Console.WriteLine("[" + s.Substring(2) + "]");
    }
}

How It Works

  1. Asking for too much. After index 1 there is only one character, but the call wants five, so it throws.
  2. Clamp the length. Math.Min(5, s.Length - 1) picks the smaller of “what I want” and “what is left”, here 1.
  3. A safe slice. With length 1, the call returns #.
  4. Start at Length. Substring(2) on a 2-character string is allowed and returns "". The brackets make the empty result visible.

Frequently Asked Questions

It returns a new string copied from part of the original. Substring(start) copies from start to the end; Substring(start, length) copies length characters.
No, it is the length. To copy from start up to end, use Substring(start, end - start).
Because startIndex is negative or past the end, or because startIndex + length is larger than the string length. Check the length first or clamp it with Math.Min.
The range operator: text[2..5] copies indexes 2, 3, and 4, and text[^3..] copies the last three characters. For strings, both create a new string just like Substring.
No. Strings are immutable, so Substring always returns a separate string and the original stays the same.

Next: ToCharArray()

Copy the characters of a string into a new char array you can change.

ToCharArray() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful