C# | Substring() Method - GeeksforGeeks

10 May, 2019
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:
String.Substring(Int32) MethodString.Substring(Int32, Int32) Method
String.Substring(Int32) Method
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));    }}
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:
if the parameters startIndex or length is less than zero.If startIndex + length indicates a position which is not within current instance.
if the parameters startIndex or length is less than zero.
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));    }}
String    : GeeksForGeeks
Sub String1: GeeksFor
Sub String2: For

References:
https://msdn.microsoft.com/en-us/library/system.string.substring1
https://msdn.microsoft.com/en-us/library/system.string.substring2
Akanksha_Rai
CSharp-method
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# | Delegates
Destructors in C#
Extension Method in C#
C# | Constructors
Introduction to .NET Framework
C# | Abstract Classes
C# | Class and Object
C# | Data Types
C# | Encapsulation
HashSet in C# with Examples