
Substring extracts strings. It requires that you indicate a start index and a length. It then returns a completely new string with the characters in that range. The C# language provides two overloaded Substring methods. Substring is ideal for getting parts of strings.
Tip:
Substring(0, 3) = substring containing the first three characters
Substring(3, 3) = substring containing the second three characters
Substring(6) = substring containing all characters after the first six

Initially here you have a string and you want to extract the first several characters into a new string. We can use the Substring instance method with two parameters here, the first being 0 and the second being the desired length.
Program that uses Substring [C#]
using System;
class Program
{
static void Main()
{
string input = "OneTwoThree";
// Get first three characters
string sub = input.Substring(0, 3);
Console.WriteLine("Substring: {0}", sub);
}
}
Output
Substring: One
Description. The Substring method is an instance method on the string class, which means you must have a non-null string to use it without triggering an exception. This program will extract the first three characters into a new string reference, which is separately allocated on the managed heap.
Continuing on, we see the Substring overloaded method that takes one parameter, the start index int. The second parameter is considered the largest possible, meaning the substring ends at the last char.
Example program that calls Substring [C#]
using System;
class Program
{
static void Main()
{
string input = "OneTwoThree";
// Indexes:
// 0:'O'
// 1:'n'
// 2:'e'
// 3:'T'
// 4:'w' ...
string sub = input.Substring(3);
Console.WriteLine("Substring: {0}", sub);
}
}
Output
Substring: TwoThreeDescription. The program describes logic that takes all the characters in the input string excluding the first three. The end result is that you extract the last several characters. The Substring method internally causes the runtime to allocate a new string on the managed heap.
In this example we take several characters in the middle of a C# string and place them into a new string. To take a middle substring, pass two integer parameters to Substring. You will want each parameter to be a non-zero value to avoid taking all the edge characters.
Example program that uses Substring [C#]
using System;
class Program
{
static void Main()
{
string input = "OneTwoThree";
string sub = input.Substring(3, 3);
Console.WriteLine("Substring: {0}", sub);
}
}
Output
Substring: TwoDescription of parameters. The two parameters in the example say, "I want the substring at index 3 with a length of three." Essentially, the third through sixth characters. The program then displays the resulting string that is pointed to by the string reference 'sub'.

We note that you can add an extension method to "slice" strings as is possible in languages such as JavaScript. The Substring method in C# doesn't use the same semantics as the Slice method from JavaScript and Python. However, you can develop an extension method that fills this need efficiently.
String SliceHere you want to not copy the last several characters of your string. This example shows how you can take the last five characters in the input string and get a new string instance containing them.
Program that uses Substring for ending characters [C#]
using System;
class Program
{
static void Main()
{
string input = "OneTwoThree";
string sub = input.Substring(0, input.Length - 5);
Console.WriteLine("Substring: {0}", sub);
}
}
Output
Substring: OneTwo
There is some reference material on the MSDN website provided by Microsoft. The Substring articles I found on MSDN are not helpful. They are not nearly as nice as this document. They do not say anything that you cannot find from Visual Studio's IntelliSense.
MSDN referenceThere are some exceptions that can be raised when the Substring instance method on the string type is called with incorrect arguments. This example triggers the ArgumentOutOfRangeException. When you try to go beyond the string length, or use an argument < 0, you get the ArgumentOutOfRangeException from the internal method InternalSubStringWithChecks.
ArgumentExceptionProgram that shows Substring exceptions [C#]
using System;
class Program
{
static void Main()
{
string input = "OneTwoThree";
try
{
string sub = input.Substring(-1);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
try
{
string sub = input.Substring(0, 100);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
Output
System.ArgumentOutOfRangeException
System.String.InternalSubStringWithChecks
System.ArgumentOutOfRangeException
System.String.InternalSubStringWithChecks
Performance is important. String-related allocations can require a lot of time in some programs. We wanted to see if taking characters and putting them into a char[] array could be faster than calling Substring. The result was that Substring is faster. However, if you want to extract only certain characters, consider the char[] approach shown.
Data tested
string s = "onetwothree"; // Input
Char array method version
char[] c = new char[3];
c[0] = s[3];
c[1] = s[4];
c[2] = s[5];
string x = new string(c); // "two"
if (x == null)
{
}
Substring version
string x = s.Substring(3, 3); // "two"
if (x == null)
{
}
Substring benchmark result
Substring was faster.
New char[] array: 2382 ms
Substring: 2053 ms [faster]Benchmark notes. The above code is simply a benchmark you can run in Visual Studio to see the performance difference of Substring and char[] arrays. It is best to use Substring when it has equivalent behavior.
Benchmark ProgramsIt is possible to take a one-character substring. However, if you simply use the string indexer to get a character, such as with value[5], you will have better performance. This is because Substring must create a whole object on the managed heap. The string indexer just returns a char, which is a value type like an integer.

We saw several examples concentrated on the Substring instance method with one or two arguments on the string type in the C# language. Additionally, we saw where to research Substring on MSDN, information about Slice, Substring exceptions, and a benchmark of Substring. Substring is useful and can help simplify your programs. It has no significant performance problems.
String Type