Home > DeveloperSection > Blogs > Substring methods in C#

Substring methods in C#


C# String  SubString 
Ratings:
1 Comment(s)
 1242  View(s)
Rate this:

Substring methods in C#

In this blog I’m explaining about Substring () method of string class in C#.

Substring:

The Substring method is used to grab characters from a string of text. For example, suppose you were testing an email address. You want to test the last four characters to see they are .com. You can use Substring to return just those four characters, and see what's in them. (You can also use IndexOf to achieve the same result.)

Substring extracts strings. It requires that you indicate a start index and a length. It then returns a completely new string with the characters in that range. We use two overloaded Substring methods.

Substring methods is overloaded

1.     public string Substring (int startIndex)

2.     public string Substring (int startIndex, int length)

 

Example

1.  public string Substring (int startIndex)

So the word you want to grab characters from goes first, followed by the Substring method. In between the round brackets, you have to tell C# where in the word to start grabbing characters from.

Retrieves a substring from this instance. The substring starts at a specified character position and continues to the end of the string.

Continuing on, we see the Substring overloaded method that takes one parameter, the start index int. The second parameter is considered the largest possible, meaning the substring ends at the last char.

namespace Substring

{

    class Program

    {

        static void Main(string[] args)

        {

            string words = "KamlakarRohitPawan";

 

            string substring = words.Substring(8);

 

            Console.WriteLine("Filter String =  "+ substring);

 

            Console.ReadLine();

        }

    }

}

 

The program describes logic that takes all the characters in the input string excluding the first three. The end result is that you extract the last several characters. The length is reduced by three.

 

Output

 

2.  public string Substring (int startIndex, int length)

Initially here you have a string and you want to extract the first part into a new string. We can use the Substring instance method with two parameters here, the first being 0 and the second being the desired length.

namespace Substring

{

    class Program

    {

        static void Main(string[] args)

        {

            string words = "KamlakarRohitPawan";

 

            string substring = words.Substring(0,8);

 

            Console.WriteLine("Filter String =  "+ substring);

 

            Console.ReadLine();

        }

    }

}

 

The Substring method is an instance method on the string class, which means you must have a non-null string to use it without triggering an exception. This program will extract the first three characters into a new string reference.

Output

 

Get Middle Char

Here we take several characters in the middle of a string and place them into a new string. To take a middle substring, pass two arguments to Substring. You will want each parameter to be a non-zero value to avoid the edge characters.

 

In this example, the two parameters say, "I want the substring at startIndex and how many character get that second parameters and then return Substring method a new string.

namespace Substring

{

    class Program

    {

        static void Main(string[] args)

        {

            string words = "KamlakarRohitPawan";

 

            string substring = words.Substring(8,5);

 

            Console.WriteLine("New String =  "+ substring);

 

            Console.ReadLine();

        }

    }

}

 

Output

 

Avoid Character

We can avoid copying the last several characters of a string. This example eliminates the last ten characters from the input string. The Substring call returns a new string without them.

namespace Substring

{

    class Program

    {

        static void Main(string[] args)

        {

            string words = "KamlakarRohitPawan";

 

            string substring = words.Substring(0,words.Length-10);

 

            Console.WriteLine("New String =  "+ substring);

 

            Console.ReadLine();

        }

    }

}

 Output

in my next post i'll explain about Display property of CSS


good

By Chintoo Semi on   11 months ago
this is good article.

Don't want to miss updates? Please click the below button!

Follow MindStick