Home
Map
String Substring ExamplesGet parts from existing strings with Substring. Pass start and length arguments.
C#
This page was last reviewed on Nov 10, 2023.
Substring. The C# Substring method extracts a fragment of a string based on character positions. We specify a start and length (both ints) to describe this fragment.
With Substring, we must be careful to pass in arguments that are within the bounds of the string. And for performance, avoiding Substring() is usually helpful.
First example. We invoke Substring to extract parts of a string into a new string. We use 1 or 2 arguments—the first is the start index, and the second is the desired length.
Part 1 We call Substring with just the starting index of the substring. Strings are indexed with the first character 0.
Part 2 We use a second argument with Substring—this is the count of characters in the substring we want.
using System; string input = "abcd"; // Part 1: get substring starting at index 2. // ... This continues until the end of the string. string result = input.Substring(2); Console.WriteLine($"RESULT: {result}"); // Part 2: use a second argument. // ... Start at index 1. // ... Continue for 2 places. string result2 = input.Substring(1, 2); Console.WriteLine($"RESULT: {result2}");
RESULT: cd RESULT: bc
Get middle part. Here we take several chars in the middle of a string and place them into a new string. To take a middle substring, pass 2 arguments to Substring.
Argument 1 The first argument to Substring is the start index. With index 3, we begin at the start of the fourth character "T."
Argument 2 This is the length of the substring we want. We specify 3 to get a three-character substring.
using System; string input = "OneTwoThree"; // Take substring. string result = input.Substring(3, 3); Console.WriteLine("RESULT: {0}", result);
RESULT: Two
Get beginning part. We can get just the first part of a string. Here we eliminate the last 2 characters from the input string. Substring() returns a new string without them.
Note This code reduces the length of the string. It will cause an error if the string is too short—a check would be needed.
String Length
Tip The Remove method can be used to remove parts of strings—it internally calls Substring with the correct arguments.
String Remove
using System; string input = "abcde"; // Take beginning part. string result = input.Substring(0, input.Length - 2); Console.WriteLine("RESULT: {0}", result);
RESULT: abc
IndexOf with Substring. The IndexOf method is made to be used with Substring (and other String methods). Here we want to parse a string by finding a separator.
Part 1 We call IndexOf to locate the position of the separator in the string. If found, the returned value is never -1.
String IndexOf
Part 2 If the separator is found, we call Substring to get the following part. We add the separator's length to the start index.
using System; string value = "Unit: 300 V"; string separator = ": "; // Part 1: get index of separator. int separatorIndex = value.IndexOf(separator); // Part 2: if separator exists, get substring. if (separatorIndex >= 0) { string result = value.Substring(separatorIndex + separator.Length); Console.WriteLine("RESULT: {0}", result); }
RESULT: 300 V
Exceptions. Substring() must be passed arguments within the range of the string. Exceptions can help us fix problems faster. This example triggers the ArgumentOutOfRangeException.
Part 1 We try to get a substring with a negative starting index—this makes no sense, and causes an error.
Part 2 We cannot take a Substring with a length past the end of the source string. This causes an ArgumentOutOfRangeException.
ArgumentException
using System; string input = "OneTwoThree"; // Part 1: try negative start index. try { string sub = input.Substring(-1); } catch (Exception ex) { Console.WriteLine(ex); } // Part 2: try excessive length. try { string sub = input.Substring(0, 100); } catch (Exception ex) { Console.WriteLine(ex); }
System.ArgumentOutOfRangeException System.String.InternalSubStringWithChecks System.ArgumentOutOfRangeException System.String.InternalSubStringWithChecks
Null substring. If a string may be null, we need to test it against null before using Substring on it. We cannot take a Substring of a null string.
Info We can use string.IsNullOrEmpty to prevent a NullReferenceException. This also checks for an empty string.
string.IsNullOrEmpty
using System; string value = null; // This is safe. if (!string.IsNullOrEmpty(value)) { Console.WriteLine(value.Substring(1)); } // This will cause an exception. Console.WriteLine(value.Substring(1));
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
One character. 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.
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
using System; string value = "cat"; // ... In many programs, we can use a char instead of Substring. Console.WriteLine(value[0]); Console.WriteLine(value.Substring(0, 1));
c c
Avoid substring. With logic, we can avoid invoking Substring. 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.
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")); } }
Win Com
Benchmark, char array. String-related allocations can be a burden. Here we see if taking characters and putting them into a char array is faster than calling Substring.
Version 1 This code creates a char array and assigns elements from the source string. Then it creates a string with a constructor.
Version 2 This version uses the Substring() method—it is shorter, simpler, and faster.
Result Substring is faster. But if we want to extract only certain characters, consider the char array approach shown.
char Array
Tip It is best to use Substring when it has equivalent behavior. Code is shorter, simpler and easier to read.
using System; using System.Diagnostics; const int _max = 1000000; const string value = "onetwothree"; // Version 1: create new string from char array. var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { char[] array = new char[3]; array[0] = value[3]; array[1] = value[4]; array[2] = value[5]; string result = new string(array); if (result == null) { return; } } s1.Stop(); // Version 2: use Substring. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { string result = value.Substring(3, 3); if (result == null) { return; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
19.19 ns new char[], new string() 13.58 ns Substring
Benchmark, one char. Suppose we have a string and we want to get a single character from it. A 1-char substring is possible, but accessing the char directly is a better option.
Version 1 This code uses a 1-char substring call to get the first letter of the word "jounce."
Version 2 This version accesses the first character with an index expression. It performs faster.
Result If your program creates 1-char substrings occasionally, it might be worth special-casing those calls to access a char instead.
using System; using System.Diagnostics; const int _max = 1000000; const string value = "jounce"; // Version 1: get 1-character substring. var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { string firstLetter = value.Substring(0, 1); if (firstLetter != "j") { return; } } s1.Stop(); // Version 2: access char directly. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { char firstLetter = value[0]; if (firstLetter != 'j') { return; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
18.07 ns Substring, 1-char 0.99 ns Access char directly
Summary. Substring() allocates a new string. We invoke it with 1 or 2 arguments—the start and length. Avoiding Substring when possible is often a worthwhile optimization.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 10, 2023 (simplify).
Home
Changes
© 2007-2024 Sam Allen.