Open In App

C# | Substring() Method

Last Updated : 10 May, 2019
Improve
Improve
Like Article
Like
Save
Share
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:



Similar Reads

C# Program to Print the Names that Contain 'MAN' Substring Using LINQ
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matter which type of data source is used. In this arti
2 min read
Difference between Method Overriding and Method Hiding in C#
Method Overriding is a technique that allows the invoking of functions from another class (base class) in the derived class. Creating a method in the derived class with the same signature as a method in the base class is called Method Overriding. In simple words, Overriding is a feature that allows a subclass or child class to provide a specific im
4 min read
C# Program to Demonstrate the Example of LINQ Intersect() Method with OrderBy() Method
LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It provides the power to .NET languages to create queries to retrieve data from the data source. In this article we will demonstrate the example of the LINQ intersect() method with OrderBy() method. 1. intersect() Method: This is used to get the common elements from the tw
3 min read
C# | Uri.MakeRelativeUri(Uri) Method
Uri.MakeRelativeUri(Uri) Method is used to determine the difference between two Uri instances. Syntax: public Uri MakeRelativeUri (Uri uri); Here, it takes the URI to compare to the current URI. Return Value: If the hostname and scheme of this URI instance and uri are the same, then this method returns a relative Uri that, when appended to the curr
3 min read
Array.GetValue() Method in C# with Examples | Set - 1
Array.GetValue() Method in C# is used to gets the value of the specified element in the current Array. There are total 8 methods in the overload list of this method which are as follows: Array.GetValue(Int32, Int32) Array.GetValue(Int64, Int64) Array.GetValue(Int32) Array.GetValue(Int64) Array.GetValue(Int32, Int32, Int32) Array.GetValue(Int64, Int
4 min read
C# | CharEnumerator.MoveNext() Method
CharEnumerator.MoveNext() Method is used to increments the internal index of the current CharEnumerator object to the next character of the enumerated string. Syntax: public bool MoveNext(); Return Value: This method returns the boolean value true value if the index is successfully incremented and within the enumerated string otherwise, false. Belo
2 min read
C# | Math.IEEERemainder() Method
In C#, IEEERemainder() is a Math class method which is used to return the remainder resulting from the division of a specified number by another specified number. Syntax: public static double IEEERemainder (double a, double b); Parameters: a: It is the dividend of type System.Double.b: It is the divisor of type System.Double. Return Type: This meth
2 min read
C# Program to Demonstrate the Use of Exit() Method of Environment Classhttps://origin.geeksforgeeks.org/?p=705454
Environment Class provides information about the current platform and manipulates, the current platform. The Environment class is useful for getting and setting various operating system-related information. We can use it in such a way that retrieves command-line arguments information, exit codes information, environment variable settings informatio
3 min read
C# | IndexOfAny() Method
In C#, IndexOfAny() method is a String Method. It is used to search the specified character index present in the string which will be the first occurrence from start index. It returns index as an integer value. This method can be overloaded by changing the number of arguments passed to it. IndexOfAny Method (Char[])IndexOfAny Method (Char[], Int32)
7 min read
C# | CharEnumerator.GetType() Method
CharEnumerator.GetType() Method is used to get the type of the current instance. This method is inherited from the Object Class. Syntax: public Type GetType(); Return Value: This method returns the exact runtime type of the current instance. Below are the programs to illustrate the use of CharEnumerator.GetType() Method:+ Example 1: // C# program t
2 min read
Article Tags :