String Function : Substring

Substring of a string

To extract the substring of a string we can make use of the method substring()

This method has two overloaded constructors.
Substring(int startIndex) this will take the starting index of the substring to be returned for example if we choose exampleString.Substring(4) , all the characters starting from the index 4 till the end of the string is returned as a sub string.

Substring(int startIndex, int length) this will  take the starting index of the substring an the count of the characters from the starting index to be returned 
for example if we choose exampleString.Substring(4, 5) , all the characters starting from the index 4 till the (4+5)th index of the string is returned as a sub string.


The Code is as below 

using System;
namespace Logiphix
{
    class Program
    {
        static void Main(string[] args)
        {
            string s;

            s = "Substring of this string";
            Console.WriteLine(s);
            Console.WriteLine(s.Substring(3)); //// this will give the string that starts from the index 3
            Console.WriteLine(s.Substring(3, 6)); //// string from the index 3 till a length of 5 characters 

            Console.ReadKey();
        }
    }
}

The output is as below:

No comments:

Post a Comment