C# Substring

Substring method example

Substring extracts strings. It requires the location of the substring—usually a start index and a length. It then returns a new string with the characters in that range. We use two overloaded Substring methods.

Substring parameters

Substring(0, 3)    Returns substring of first 3 chars.
Substring(3, 3)    Returns substring of second 3 chars.
Substring(6)       Returns substring of all chars after first 6.

First part

Initially here we have a string. We want to extract the first part of it into a new string. We can use the Substring method with two parameters—the first is 0 and the second is the desired length.

First

Argument 1:The starting index of the substring. Please remember strings are indexed with the first character 0.

Argument 2:The length of the substring part. This is not the final index, but the count of characters in the substring we want.

Length
Based on:

.NET 4.5

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

One parameter

Program icon

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.

Program:The program describes logic that takes all the characters in the input string excluding the first three.

Result:The end result is that you extract the last several characters. The length is reduced by three.

Program that calls Substring, one argument: 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: TwoThree

Middle chars

Char type example: letter C

Here we take several characters in the middle of a string and place them into a new string. To take a middle substring, pass two arguments to Substring. You will want each parameter to be a non-zero value to avoid the edge characters.

Parameters:In this example, the two parameters say, "I want the substring at index 3 with a length of three."

Program that uses Substring, two arguments: C#

using System;

class Program
{
    static void Main()
    {
	string input = "OneTwoThree";

	string sub = input.Substring(3, 3);
	Console.WriteLine("Substring: {0}", sub);
    }
}

Output

Substring: Two

Avoid chars

Program

We can avoid copying the last several characters of a string. This example eliminates the last five characters from the input string. The Substring call returns a new string without them.

Info:This method reduces the length of a string. It will cause an error if the string is too short—a check would be needed.

Program that uses Substring, 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

Notation

Slice

Other languages use different arguments for substring. For example, in Java the start index and the end index are specified—the length of the substring is not needed. This is similar to the slice syntax.

Substring, Java

Slice:In Python and JavaScript, slice notation is often used.
We can use relative indexes.

PythonProgramming tip

An extension. We can add an extension method to "slice" strings as is possible in other languages.
So you can specify indexes,
as in Java
or Python,
to get substrings in your C# program. An extension method efficiently fills this need.

String Slice

Research

ABC: letters

I found some reference material on the MSDN website provided by Microsoft. The Substring articles on MSDN are not helpful. They do not say anything that you cannot find from Visual Studio's IntelliSense.

String.Substring Method: MSDN

Exceptions

Error

There 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.

Tip:When you try to go beyond the string length, or use an argument < 0, you get an ArgumentOutOfRangeException.

ArgumentException
Program that shows 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

Benchmark

Performance

Performance is important. String-related allocations can require a lot of time in some programs. I wanted to see if taking characters and putting them into a char array is faster than calling Substring.

Result:Substring is faster. But if you want to extract only certain characters, consider the char[] approach shown.

Char Array

Tip:It is best to use Substring when it has equivalent behavior. Code is shorter, simpler and easier to read.

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

New char[] array: 2382 ms
Substring:        2053 ms [faster]

One character

Char type

It is possible to take a one-character substring. But if we simply use the string indexer to get a character, we will have better performance. And sometimes the improvement is significant.

Note:Substring creates an object on the heap. The string indexer just returns a char, which is an integer-like value—this is faster.

Char
Program that uses char, Substring: C#

using System;

class Program
{
    static void Main()
    {
	string value = "cat";
	// ... In many programs, we can use a char instead of Substring.
	Console.WriteLine(value[0]);
	Console.WriteLine(value.Substring(0, 1));
    }
}

Output

c
c

Logic

Logic

With logic we can avoid invoking Substring—this improves performance in certain programs. Suppose a program gets the same Substring over and over again. We can handle this case in code, and return a literal.

Here:I introduce simple code in SubstringFirst3 that optimizes the case of getting the first 3 letters of the string "Windows."

So:In a program that happens to do this operation many times, this logic would reduce allocations and increase speed.

Program that avoids Substring: C#

using System;

class Program
{
    static string SubstringFirst3(string value)
    {
	// ... Use logic to avoid creating a new string.
	if (value == "Windows")
	{
	    return "Win";
	}
	else
	{
	    return value.Substring(0, 3);
	}
    }

    static void Main()
    {
	Console.WriteLine(SubstringFirst3("Windows"));
	Console.WriteLine(SubstringFirst3("Computer"));
    }
}

Output

Win
Com

Summary

C# language

Substring allocates a new string. We called it with one or two arguments. With MSDN we researched this method.
Finally we learned about Slice,
Substring exceptions,
and Substring performance.


C#: String