Open In App

C# | Substring() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In C#, Substring() is a string method. It is used to retrieve a substring from the current instance of the string. This method can be overloaded by passing the different number of parameters to it as follows:

  1. String.Substring(Int32) Method
  2. String.Substring(Int32, Int32) Method

String.Substring Method (startIndex)

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

Syntax:

public string Substring(int startIndex)
  • Parameter: This method accept one parameter “startIndex”. This parameter will specify the starting position of the substring which has to be retrieve. The type of this parameter is System.Int32.
  • Return Value: This method will return the substring which begins from startIndex and continues to the end of the string. The return value type is System.String.

Exception: If startIndex is less than zero or greater than the length of current instance then it will arise ArgumentOutOfRangeException.

Example:

Input : str  = "GeeksForGeeks"
        str.Substring(5);
Output: ForGeeks

Input : str  = "GeeksForGeeks"
        str.Substring(8);
Output: Geeks

Below program illustrate the above-discussed method:




// C# program to demonstrate the 
// String.Substring Method (startIndex)
using System;
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // define string
        String str = "GeeksForGeeks";
  
        Console.WriteLine("String    : " + str);
  
        // retrieve the substring from index 5
        Console.WriteLine("Sub String1: " + str.Substring(5));
  
        // retrieve the substring from index 8
        Console.WriteLine("Sub String2: " + str.Substring(8));
    }
}


Output:

String    : GeeksForGeeks
Sub String1: ForGeeks
Sub String2: Geeks

String.Substring Method (int startIndex, int length)

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

Syntax :

public string Substring(int startIndex, int length)
  • Parameter: This method accept two parameters “startIndex” and length. First parameter will specify the starting position of the substring which has to be retrieve and second parameter will specify the length of the substring. The type of both the parameters is System.Int32.
  • Return Value: This method will return the substring which begins from specified position and substring will have a specified length. The return value type is System.String.

Exception: This method can arise ArgumentOutOfRangeException in two conditions:

  1. if the parameters startIndex or length is less than zero.
  2. If startIndex + length indicates a position which is not within current instance.

Example:

Input : str  = "GeeksForGeeks"
        str.Substring(0,8);
Output: GeeksFor

Input : str  = "GeeksForGeeks"
        str.Substring(5,3);
Output: For

Input : str  = "Geeks"
        str.Substring(4,0);
Output: 

Below program illustrate the above-discussed method:




// C# program to demonstrate the 
// String.Substring Method 
// (int startIndex, int length)
using System;
class Geeks {
  
    // Main Method
    public static void Main()
    {
  
        // define string
        String str = "GeeksForGeeks";
  
        Console.WriteLine("String    : " + str);
  
        // retrieve the substring from index 0 to length 8
        Console.WriteLine("Sub String1: " + str.Substring(0, 8));
  
        // retrieve the substring from index 5 to length 3
        Console.WriteLine("Sub String2: " + str.Substring(5, 3));
    }
}


Output:

String    : GeeksForGeeks
Sub String1: GeeksFor
Sub String2: For

References:



Last Updated : 10 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads