find and solve || findandsolve.com
Please wait.....

C# String Substring Method

In the C#, Substring() is a string method which is used to retrieve a substring from the current instance of the string.If you want to get the particular length of the substring, then you can specify the length of the substring to return from the given string.


C# Substring Method Syntax

In the give below  syntax, defining a Substring method to get a substring from the given string in the c# language.

public string Substring(Int32 startIndex)  

public string Substring(Int32, Int32)  
Parameter

startIndex: it is an integer type parameter which is used to pass index to get a substring.

Return

It returns a string


C# Substring Method Example

In the given example of using the Substring() method to get a substring starting from the given string in the c# language

Example
    using System;
    public class StringContainsExample
    {         public static void Main(string[] args)
        {
          string str = "Hello to FindAndSolve";
          Console.WriteLine("SubString: {0}", str.Substring(4));
          Console.WriteLine("Substring with Length: {0}", str.Substring(2, 5));
          Console.ReadLine();
        }
    }    

output
SubString: o to FindAndSolve
Substring with Length: llo t

In the given below example, you used a Substring() method to get a substring starting from position 4 till the end of the defined string and another substring starting from position 2, and the length of the substring should be 5 characters.