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) 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.
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
Recommended Posts:
- Difference between Method Overriding and Method Hiding in C#
- C# | Uri.MakeRelativeUri(Uri) Method
- Array.GetValue() Method in C# with Examples | Set - 1
- C# | CharEnumerator.MoveNext() Method
- C# | Math.IEEERemainder() Method
- C# | IndexOfAny() Method
- C# | CharEnumerator.GetType() Method
- File.GetLastWriteTimeUtc() Method in C# with Examples
- MathF.Sin() Method in C# with Examples
- Double.CompareTo Method in C# with Examples
- C# | Graphics.DrawLine() Method | Set - 1
- UInt16.GetHashCode Method in C# with Examples
- Stack.Peek Method in C#
- C# | IsNullOrEmpty() Method
- C# | Math.Sign() Method
- Int64.CompareTo Method in C# with Examples
- C# | PadRight() Method
- How to use Array.BinarySearch() Method in C# | Set -1
- C# | EndsWith() Method
- C# | Char.CompareTo() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai

