Home

Search

C# Substring Examples

Get parts from existing strings with Substring. Pass start and length arguments.

dot net perls

Substring. From above, an eagle views the land. It perceives great detail. But it does not see all—what is visible is just a slice (a fragment) of what exists below.

Method details. With Substring we extract a fragment of an existing string. A start and length (both ints) describe this view. Like an eagle's view, Substring() considers only a part.

Initial 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 A: We call Substring with just the starting index of the substring. Strings are indexed with the first character 0.
Part B: We use a second argument with Substring—this is the count of characters in the substring we want.
C# program that uses Substring using System; class Program { static void Main() { string input = "abcd"; // Part A: get substring starting at index 2. // ... This continues until the end of the string. string result = input.Substring(2); Console.WriteLine($"RESULT: {result}"); // Part B: use a second argument. // ... Start at index 1. // ... Continue for 2 places. string result2 = input.Substring(1, 2); Console.WriteLine($"RESULT: {result2}"); } } Output 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.
Info: In this code, the 2 parameters say, "I want the substring at index 3 with a length of 3 characters."
C# program that uses Substring, two arguments using System; class Program { static void Main() { string input = "OneTwoThree"; // Take substring. string result = input.Substring(3, 3); Console.WriteLine("RESULT: {0}", result); } } Output 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.
Remove
C# program that uses Substring to get beginning part using System; class Program { static void Main() { string input = "abcde"; // Take beginning part. string result = input.Substring(0, input.Length - 2); Console.WriteLine("RESULT: {0}", result); } } Output 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.
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.
C# program that uses IndexOf with Substring using System; class Program { static void Main() { 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); } } } Output 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 A: Here we try to get a substring with a negative starting index—this makes no sense, and causes an error.
Part B: We cannot take a Substring with a length past the end of the source string. This causes an ArgumentOutOfRangeException.
ArgumentException
C# program that shows exceptions using System; class Program { static void Main() { string input = "OneTwoThree"; // Part A: try negative start index. try { string sub = input.Substring(-1); } catch (Exception ex) { Console.WriteLine(ex); } // Part B: try excessive length. try { string sub = input.Substring(0, 100); } catch (Exception ex) { Console.WriteLine(ex); } } } Output 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.
IsNullOrEmpty, IsNullOrWhiteSpace
C# program that causes NullReferenceException using System; class Program { static void Main() { 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)); } } Output 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
C# program that uses char, Substring 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

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.
C# program that avoids Substring 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

All substrings. Is one string contained in another? This can be answered with IndexOf. But we can also extract all possible substrings, put them in a Dictionary, and use ContainsKey.
And: This approach can be many times faster than using IndexOf, but only works in certain cases.
Dictionary: If we were to place these substrings in a Dictionary, use the substring for the key, and the original string for the value.
Dictionary
Important: Do not attempt this on a long string, or memory usage might explode. A tree data structure can be an even better solution.
Here: We generate all substrings for an input string. These substrings can be used as keys in a Dictionary.
C# program that generates all substrings using System; class Program { static void Main() { string value = "abcdefghi"; // Avoid full length. for (int length = 1; length < value.Length; length++) { // Be careful with the end index. for (int start = 0; start <= value.Length - length; start++) { string substring = value.Substring(start, length); Console.WriteLine(substring); } } } } Output a b c d e f g h i ab bc cd de ef fg gh hi abc bcd cde def efg fgh ghi abcd bcde cdef defg efgh fghi abcde bcdef cdefg defgh efghi abcdef bcdefg cdefgh defghi abcdefg bcdefgh cdefghi abcdefgh bcdefghi

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.
C# program that benchmarks new string from char array using System; using System.Diagnostics; class Program { const int _max = 1000000; static void Main() { 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")); } } Output 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.
C# program that tests one-char substring performance using System; using System.Diagnostics; class Program { const int _max = 1000000; static void Main() { 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")); } } Output 18.07 ns Substring, 1-char 0.99 ns Access char directly

Notation. 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.
Slice: In Python and JavaScript, slice notation is often used. We can use relative indexes.

An extension. We can add an extension method to "slice" strings. So you can specify indexes, as in Java or Python, to get substrings in your C# program.String Slice

Rewrite Split. Internally the Split method finds Substrings and places them into a string array. With Substring and IndexOf we can duplicate this logic.Split
Sometimes: We only need a single part of a large string. If we avoid Split in this case, we can avoid creating many strings.
Bugs: This style of optimization can yield code that is fast but prone to bugs. Be prepared to fix problems.

First words. A string contains important words at its start. With a loop that counts spaces, we can extract the first words from a string into a new string.First WordsFirst Sentence

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

Home

© 2007-2020 sam allen. send bug reports to info@dotnetperls.com.