Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

C# String.Substring() method

Reference for C# String.Substring(): extract part of a string by start index and length, the exceptions it throws, and range slicing as a modern alternative.

Substring returns a new string that contains part of the original. You give a start index and, optionally, a length. Without a length you get everything from the start to the end of the string.

It copies the characters, so each call allocates a new string. The original string never changes. In modern C# the range syntax s[2..5] does the same job with shorter code, and AsSpan avoids the copy altogether.

C#
string s = "Hello, World";
Console.WriteLine(s.Substring(7));      // World
Console.WriteLine(s.Substring(0, 5));   // Hello

Try it

C# Start and length

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Errors and safe patterns

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public string Substring(int startIndex)
public string Substring(int startIndex, int length)
Name Description
startIndex Zero-based position of the first character to take. May equal Length (result is empty).
length Number of characters to take. Must not run past the end of the string.

Returns: a new string. If startIndex is 0 and the length covers the whole string, the original instance is returned. Empty results are "", never null.

Errors: ArgumentOutOfRangeException if startIndex is negative or greater than Length, if length is negative, or if startIndex + length is greater than Length. NullReferenceException if the string itself is null.

Usage notes

  • The second argument is a length, not an end index. This differs from s[2..5] and from JavaScript's substring. s.Substring(2, 3) is the same as s[2..5].
  • Strict bounds. Unlike some languages, C# does not clamp. Check the length first, or use a helper like Left above.
  • Starting at Length is legal and yields an empty string, which makes loops over "the rest of the string" safe.
  • UTF-16 units. Cutting in the middle of a surrogate pair (an emoji, for example) produces an invalid string. For user-visible truncation prefer text elements; see Text elements.
  • Performance. Each call allocates and copies O(length) characters. In parsing code, slice a ReadOnlySpan<char> and convert to string only at the end.
  • Case and culture do not matter: Substring copies characters exactly.
  • Finding the positions usually comes from IndexOf or Split. If you only need the pieces around a separator, Split is shorter than manual IndexOf code.
  • Ranges need Length and Substring(int, int) on the type, which string has, so s[a..b] compiles to a Substring call.