Open In App

C# | Substring() Method

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)

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)

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:


Article Tags :
C#