;

Substring() Method in C#


Tutorialsrack 09/08/2021 C#

In this article, you’ll learn what is Substring in C# and how to use the Substring() method in C#. 

What is Substring in C#?

A Substring in C# is a contiguous sequence of characters within a string. In other words, a substring is a portion of a string. The String class in C# represents a string. For instance, "the best of" is a substring of "It was the best of times". 

Substring() method in C#

In C#, the Substring() method is a built-in string method and it is used to get or retrieve the substring from the instance of the string. The Substring() method can be overloaded by passing the different number of parameters to it as follows:

  • String.Substring(Int32) Method
  • String.Substring(Int32, Int32) Method

1. String.Substring(Int32) Method

This method is used to retrieve a substring from the current instance of the string. The parameter “startIndex” will specify the starting position of the substring and then the substring will continue to the end of the string.

2. String.Substring Method (int startIndex, int length)

This method is used to retrieve a substring that begins from a specified position described by a parameter startIndex and has a specified length of the string. If startIndex is equal to the length of the string and parameter length is zero, then it will return nothing.

Here is a list of the most commonly asked questions related to substring in C#.

  1. How to get a substring of first n characters from a string in C#? 
  2. How to get a substring after a specific position from a string in C#?
  3. How to get a substring after a character in C#? 
  4. How to get a substring before a character in C#? 
  5. How to get a substring with start and end index in C#? 
  6. How to get a substring from index to end in C#?
  7. How to get a substring between two strings in #?
  8. How do I get the last 5 characters of the string in C#?

Example 1: How to get a substring of first n characters from a string in C#?  

In this example, you will learn how to get a substring of the first N characters from a string using a Substring() method in C#. String characters start from zero indexes. This means the position of the first characters in a string starts at the 0th position. 

For instance, suppose you want to get a substring of the first 9 characters from a string. To achieve this, we used the C# Substring() method and pass 0 as the starting index and 9 as the length of the substring to get the first 9 characters substring from a string.

Here is the example to get the substring of first N characters from a string using a Substring() method in C#.

Example 1: How to get a substring of first n characters from a string in C#?  
using System;

public class Program {
    public static void Main() {
        // A long string    
        string str = "The quick brown fox jumps over the lazy dog";
        Console.WriteLine("Original String: " + str);

        //To get first 9 characters substring from a string    
        string substring = str.Substring(0, 9);
        Console.WriteLine("\nOutput Substring: " + substring);
    }
}
Output

Original String: The quick brown fox jumps over the lazy dog

 

Output Substring: The quick

Example 2: How to get a substring after a specific position from a string in C#?

In this example, you will learn how to get a substring after a specific position from a string in C#.

For instance, suppose you want to retrieve a substring after the first 9 characters from a string. To achieve this, You can simply pass the starting position in the Substring() method to get the rest of the string as a substring. 

Here is the example to get the substring after the specific position from a string using a Substring() method in C#.

Example 2: How to get a substring after a specific position from a string in C#?
using System;

public class Program {
    public static void Main() {
        // A long string    
        string str = "The quick brown fox jumps over the lazy dog";
        Console.WriteLine("Original String: " + str);

        //To get everything else after 12th position    
        string substring = str.Substring(9);
        Console.WriteLine("\nOutput Substring: " + substring);
    }
}
Output

Original String: The quick brown fox jumps over the lazy dog

 

Output Substring:  brown fox jumps over the lazy dog

Example 3: How to get a substring after a specific character in C#?

In this example, you will learn how to get a substring after a specific character using a Substring() method in C#. 

For instance, suppose you want to retrieve a substring after a specific character from a string. To achieve this, You can simply pass the starting position of a specific character in the Substring() method to get the rest of the string as a substring. 

Here is the example to get the substring after the specific character from a string using a Substring() method in C#.

Example 3: How to get a substring after a specific character in C#?
using System;
 
public class Program {
    public static void Main() {
        // A long string    
        string str = "Superman, Batman, Aquaman:Spiderman, Ironman, Antman";
        Console.WriteLine("Original String: " + str);
 
        //To get everything else after specific character position
        /* and we add 1, so that substring start from next position
           from character position to eliminate specific character
           from substring */
        string substring = str.Substring(str.IndexOf(":") + 1);
        Console.WriteLine("\nOutput Substring: " + substring);
    }
}
Output

Original String: Superman, Batman, Aquaman:Spiderman, Ironman, Antman

 

Output Substring: Spiderman, Ironman, Antman

Example 4: How to get a substring before a specific character in C#?

In this example, you will learn how to get a substring after a specific character using a Substring() method in C#. 

For instance, suppose you want to retrieve a substring before a specific character from a string. To achieve this, we used the Substring() method and pass 0 as the starting index and length as the position of the specified characters.

Here is the example to retrieve the substring before the specific character from a string using a Substring() method in C#.

Example 4: How to get a substring before a specific character in C#?
using System;
 
public class Program {
    public static void Main() {
        // A long string    
        string str = "Superman, Batman, Aquaman:Spiderman, Ironman, Antman";
        Console.WriteLine("Original String: " + str);
 
        //To get everything else before specific character position
        string substring = str.Substring(0, str.IndexOf(":"));
        Console.WriteLine("\nOutput Substring: " + substring);
    }
}
Output

Original String: Superman, Batman, Aquaman:Spiderman, Ironman, Antman

 

Output Substring: Superman, Batman, Aquaman

Example 5: How to get a substring with start and end index in C#? 

In this example, you will learn how to get a substring between two specific indexes using a Substring() method in C#.

For instance, suppose you want to retrieve a substring that starts from the 10th position and till to the next 10 characters. To achieve this, pass the starting index as and 10 as the length of the substring to get the substring from a string.

Here is the example to retrieve the substring before the specific character from a string using a Substring() method in C#.

Example 5: How to get a substring with start and end index in C#? 
using System;
 
public class Program {
    public static void Main() {
        // A long string    
        string str = "The quick brown fox jumps over the lazy dog";
        Console.WriteLine("Original String: " + str);
 
        int startIndex = 10;
        // Get next 10 characters from starting index    
        string substring = str.Substring(startIndex, 10);
        Console.WriteLine("\nOutput Substring: " + substring);
    }
}
Output

Original String: The quick brown fox jumps over the lazy dog

 

Output Substring: brown fox

Example 6: How to get a substring from index to end in C#?

In this example, you will learn how to get a substring from a specific index to an end position using Substring() method in C#.

For instance, suppose you want to retrieve the substring from the 10th position to the end of the string. To achieve this, you need to pass the start index as 10 and the end index as the length. And to get an end index, you need to subtract the startIndex from the length of the string.

Here is the example to retrieve the substring from a specific index to end position using a Substring() method in C#.

Example 6: How to get a substring from index to end in C#?
using System;

public class Program {
    public static void Main() {
        // A long string    
        string str = "The quick brown fox jumps over the lazy dog";
        Console.WriteLine("Original String: " + str);

        int startIndex = 10;
        int endIndex = str.Length - 10;
        // To get a substring from Specific Starting index to an end index
        string substring = str.Substring(startIndex, endIndex);
        Console.WriteLine("\nOutput Substring: " + substring);
    }
}
Output

Original String: The quick brown fox jumps over the lazy dog

 

Output Substring: brown fox jumps over the lazy dog

Example 7: How to get a substring between two strings in C#?

In this example, you will learn how to get a substring between two strings in C#.

Take this sentence as a string example as given below:

The quick brown fox jumps over the lazy dog

For instance, suppose, you want a substring between the string such as “quick” and “over”. To achieve this, first, you need to find the position of both strings. Then use the position of the first string as the StartIndex and find the length of the string by subtracting the position of the first string from the position of the second string. 

Here is the example to retrieve the substring between two strings using a Substring() method in C#.

Example 7: How to get a substring between two strings in C#?
using System;

public class Program {
    public static void Main() {
        // A long string    
        string str = "The quick brown fox jumps over the lazy dog";
        Console.WriteLine("Original String: " + str);

        // Get a substring between two strings
        /* we add the "quick".Length to the "firstStringPosition" so that
           we remove eliminate the "quick" from the start and the get the 
           substring between these two Substrings*/
        int firstStringPosition = str.IndexOf("quick") + "quick".Length;
        int secondStringPosition = str.IndexOf("over");

        string substring = str.Substring(firstStringPosition,
            secondStringPosition - firstStringPosition);

        Console.WriteLine("\nOutput Substring: " + substring);
    }
}
Output

Original String: The quick brown fox jumps over the lazy dog

 

Output Substring:  brown fox jumps

Example 8: How do I get the last 5 characters of string in C#?

In this example, you will learn how to get the 5 characters or the last nth character from the string in C#. There are many ways to achieve this. In this example, we used the Substring() method and Range operator feature which is available in C# version 8.0 or higher.

Here are the examples to retrieve the last 5 characters or the last nth character from a string in C#.

Example 8.1: Using Substring() Method
using System;

public class Program {
    public static void Main() {
        // A long string    
        string str = "The quick brown fox jumps over the lazy dog";
        Console.WriteLine("Original String: " + str);

        //To get last 5 characters from a string
        var substring = str.Substring(str.Length - 5);
        Console.WriteLine("\nOutput Substring: " + substring);
    }
}
Output

Original String: The quick brown fox jumps over the lazy dog

 

Output Substring: y dog

Example 8.2: Using Substring() Method
using System;

public class Program {
    public static void Main() {
        // A long string    
        string str = "The quick brown fox jumps over the lazy dog";
        Console.WriteLine("Original String: " + str);

        // Get a last 5 character from a string
        string substring = str[ ^ 5..];

        Console.WriteLine("\nOutput Substring: " + substring);
    }
}
Output

Original String: The quick brown fox jumps over the lazy dog

 

Output Substring: y dog

In this article, I covered most of the commonly asked questions related to the substring in C#.  I hope this article will help you to understand what is Substring in C# and how to use substring() method in C#. 

Share your valuable feedback, please post your comment at the bottom of this article. Thank you!


Related Posts



Comments

Recent Posts
Tags