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# 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
Parameter
Description
startIndex
Required. The zero-based position of the first character to copy.
length
Optional. How many characters to copy. Leave it out to copy to the end.
Return Value
Return Value
Description
string
A 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));
}
}
Output
World
Hello
Wor
How It Works
Find the start.Hello is 0 to 4, the comma is 5, the space is 6, so W is at index 7.
To the end. With one argument, Substring(7) copies World.
A fixed count.Substring(0, 5) takes five characters from the front.
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);
}
}
Output
name
Sam
How It Works
Locate the divider.name takes 0 to 3, so the = is at index 4.
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.
Right side.Substring(eq + 1) skips the = and copies the rest, Sam.
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) + "]");
}
}
Output
ArgumentOutOfRangeException
#
[]
How It Works
Asking for too much. After index 1 there is only one character, but the call wants five, so it throws.
Clamp the length.Math.Min(5, s.Length - 1) picks the smaller of “what I want” and “what is left”, here 1.
A safe slice. With length 1, the call returns #.
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.