C# Substring with Examples

How to get a substring from a string in C#

The C# Substring method is a method of the String class used to extract a specified string from a string.

Learn more:

The Substring method is defined as follows:

The length argument specifies the number of characters to extract.

The Substring argument specifies the starting character position of the string to be extracted from 0. 

string.Substring(int startIndex, int length)

Example:

string str = "C# programming";
string substring1 = str.Substring(2);
string substring2 = str.Substring(1, 5);

Extract a substring with the String Substring method

This sample code uses the Substring method to extract a portion of a substring from the string str, specifying the starting position and the number of characters.

C# substring from left

Sample code:

  1. using System;
  2. namespace CSharpSubstringExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string str = "Let's learn C# programming language!";
  9. string substring1 = str.Substring(3),
  10. substring2 = str.Substring(8),
  11. substring3 = str.Substring(5, 10);
  12. Console.WriteLine("substring1: {0}", substring1);
  13. Console.WriteLine("substring2: {0}", substring2);
  14. Console.WriteLine("substring3: {0}", substring3);
  15. }
  16. }
  17. }

Output:

substring1: 's learn C# programming language!
substring2: arn C# programming language!
substring3:  learn C#

C# substring from right

Sample code:

  1. using System;
  2. namespace CSharpSubstringExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string str = "Let's_learn_C#_programming_language!";
  9. int strLength = str.Length;
  10. //Extract the substring from right
  11. string substring1 = str.Substring(strLength - 1);
  12. string substring2 = str.Substring(strLength - 2);
  13. string substring3 = str.Substring(strLength - 3);
  14. Console.WriteLine("substring1: {0}", substring1);
  15. Console.WriteLine("substring2: {0}", substring2);
  16. Console.WriteLine("substring3: {0}", substring3);
  17. }
  18. }
  19. }

Output:

substring1: !
substring2: e!
substring3: ge!

Combination with IndexOf method

In the previous sample code, the starting position and the number of characters of the substring to be extracted are specified in advance by numerical values.

However, if the extracted string can be changed using a variable, the starting position of the string will also be changed.

In that case, use the IndexOf method to obtain the starting position of the string. 

Let's see how to combine the Substring and IndexOf methods to extract a given string:

  1. using System;
  2. namespace CSharpSubstringExample
  3. {
  4. class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. string str = "Let's learn C# programming language!";
  9. Console.WriteLine("substring: {0}", GetSubstring(str, "programming"));
  10. Console.WriteLine("substring: {0}", GetSubstring(str, "learn C#"));
  11. Console.WriteLine("substring: {0}", GetSubstring(str, "C programming"));
  12. }
  13. static string GetSubstring(string str, string substring)
  14. {
  15. int length = substring.Length;
  16. int index = str.IndexOf(substring);
  17. if (index != -1)
  18. return str.Substring(index, length);
  19. return "";
  20. }
  21. }
  22. }

Output:

substring: programming
substring: learn C#
substring:

Summary

Here we have discussed the Substring method of the String class.

The Substring method is often used in combination with the IndexOf method.


See also:
C# List Contains with Examples
C# typeof with Examples
C# TryParse (int, double, float) with Examples
C# Data Types
C# Variables