Top 10 examples of SubString methods of string in CSharp

Top 10 examples of SubString in C#

Top 10 examples of SubString in C#

Hello folks, after long time I am back with simple but so important concept of SubString with examples. So let’s understand using Top 10 examples of SubString in C# as below:

  1. Get all characters after nth index from a string in C#
  2. Get a SubString of first n characters from a string
  3. Get a SubString before a character
  4. Get a SubString before a string
  5. Get a SubString after a character
  6. Get a SubString after a string
  7. Get a SubString with start and end index
  8. Get a SubString from index to end
  9. Get a SubString between two characters
  10. Get a SubString between two strings

Let’s understand understand one by one methods.

1. Get all characters after nth index from a string in C#

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";

//1. Get all characters after nth index from a string in C#
string strFirstChars = strData.Substring(10);
Console.WriteLine("1. Get all characters after nth character from a string in C#.");
Console.WriteLine(strFirstChars);

Output:

1. Get all characters after nth character from a string in C#.
e technothirsty.com website. Let's understand Substring in string from technothirsty.com website.

2. Get a SubString of first n characters from a string

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";
 
//2. Get a substring of first n characters from a string 
string strFirstChars = strData.Substring(0, 30);
Console.WriteLine("\n\n2. Get a substring of first n characters from a string.");
Console.WriteLine(strFirstChars);

Output:

2. Get a substring of first n characters from a string.
This is the technothirsty.com

3. Get a SubString before a character

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";

//3. Get a substring before a character
string strFirstChars = strData.Substring(0, strData.IndexOf("'"));
Console.WriteLine("\n\n3. Get a substring before a character/string.");
Console.WriteLine(strFirstChars);

Output:
3. Get a substring before a character/string.
This is the technothirsty.com website. Let

4. Get a SubString before a string

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";

//4. Get a substring before a string
string strFirstChars = strData.Substring(0, strData.IndexOf("'s"));
Console.WriteLine("\n\n4. Get a substring before a string.");
Console.WriteLine(strFirstChars);

Output:
4. Get a substring before a string.
This is the technothirsty.com website. Let

5. Get a SubString after a character

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";

//5. Get a substring after a character
string strFirstChars = strData.Substring(strData.IndexOf("."));
Console.WriteLine("\n\n5. Get a substring after a character.");
Console.WriteLine(strFirstChars);

Output:
5. Get a substring after a character.
.com website. Let's understand Substring in string from technothirsty.com website.

6. Get a SubString after a string

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";
              
//6. Get a substring after a string
String strFirstChars = strData.Substring(strData.IndexOf("the"));
Console.WriteLine("\n\n6. Get a substring after a string.");
Console.WriteLine(strFirstChars);

Output:
6. Get a substring after a string.
the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.

7. Get a SubString with start and end index

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";

//7. Get a substring with start and end index 
string strFirstChars = strData.Substring(20, 40);
Console.WriteLine("\n\n7. Get a substring with start and end index.");
Console.WriteLine(strFirstChars);

Output:
7. Get a substring with start and end index.
irsty.com website. Let's understand Subs

8. Get a SubString from index to end

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";
              
//8. Get a substring from index to end
int iStartIndex = 5;
string strFirstChars = strData.Substring(iStartIndex, strData.Length - iStartIndex);
Console.WriteLine("\n\n8. Get a substring from index to end.");
Console.WriteLine(strFirstChars);

Output:
8. Get a substring from index to end.
is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.

9. Get a SubString between two characters

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";

//9. Get a substring between two characters
string strFirstChars = strData.Substring(strData.IndexOf("."), strData.IndexOf("'") - strData.IndexOf("."));
Console.WriteLine("\n\n9. Get a substring between two characters.");
Console.WriteLine(strFirstChars);

Output:
9. Get a substring between two characters.
.com website. Let

10. Get a SubString between two strings

string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";
             
//10. Get a substring between two strings
string strFirstChars = strData.Substring(strData.IndexOf("website."), strData.IndexOf("from") - strData.IndexOf("website."));
Console.WriteLine("\n\n10. Get a substring between two strings.");
Console.WriteLine(strFirstChars);

Output:
10. Get a substring between two strings.
website. Let's understand Substring in string

Below code snippet contains all examples and output

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SubString
{
    class Program
    {
        static void Main(string[] args)
        {
            string strData = "This is the technothirsty.com website. Let's understand Substring in string from technothirsty.com website.";


            //1. Get all characters after nth index from a string in C#
            string strFirstChars = strData.Substring(10);
            Console.WriteLine("1. Get all characters ater nth character from a string in C#.");
            Console.WriteLine(strFirstChars);

            //2. Get a substring of first n characters from a string 
            strFirstChars = strData.Substring(0, 30);
            Console.WriteLine("\n\n2. Get a substring of first n characters from a string.");
            Console.WriteLine(strFirstChars);

            //3. Get a substring before a character
            strFirstChars = strData.Substring(0, strData.IndexOf("'"));
            Console.WriteLine("\n\n3. Get a substring before a character/string.");
            Console.WriteLine(strFirstChars);

            //4. Get a substring before a string
            strFirstChars = strData.Substring(0, strData.IndexOf("'s"));
            Console.WriteLine("\n\n4. Get a substring before a string.");
            Console.WriteLine(strFirstChars);

            //5. Get a substring after a character
            strFirstChars = strData.Substring(strData.IndexOf("."));
            Console.WriteLine("\n\n5. Get a substring after a character.");
            Console.WriteLine(strFirstChars);

            //6. Get a substring after a string
            strFirstChars = strData.Substring(strData.IndexOf("the"));
            Console.WriteLine("\n\n6. Get a substring after a string.");
            Console.WriteLine(strFirstChars);


            //7. Get a substring with start and end index 
            strFirstChars = strData.Substring(20, 40);
            Console.WriteLine("\n\n7. Get a substring with start and end index.");
            Console.WriteLine(strFirstChars);

            //8. Get a substring from index to end
            int iStartIndex = 5;
            strFirstChars = strData.Substring(iStartIndex, strData.Length - iStartIndex);
            Console.WriteLine("\n\n8. Get a substring from index to end.");
            Console.WriteLine(strFirstChars);

            //9. Get a substring between two characters
            strFirstChars = strData.Substring(strData.IndexOf("."), strData.IndexOf("'") - strData.IndexOf("."));
            Console.WriteLine("\n\n9. Get a substring between two characters.");
            Console.WriteLine(strFirstChars);

            //10. Get a substring between two strings
            strFirstChars = strData.Substring(strData.IndexOf("website."), strData.IndexOf("from") - strData.IndexOf("website."));
            Console.WriteLine("\n\n10. Get a substring between two strings.");
            Console.WriteLine(strFirstChars);


            Console.ReadKey();
        }
    }
}

Output:

Top 10 examples of SubString methods of string in CSharp

Folks I have shared important Top 10 examples of SubString in C# with code snippet and output. Kindly comment if you have another method or better way of implementation of SubString, so we could include other method’s implementation in this example.

Happy Coding!!!

Leave a Reply