Hello everyone, Here I am going to write a Program to find all substrings from a String. This example code iterate through all possible lengths of a substring.

C# Program to find all substrings from a String

Here is the source code to find all substrings from a String.

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


class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a String");

string sInput = Console.ReadLine(); ;
// Avoid full length.
for (int length = 1; length < sInput.Length; length++)
{
// End index is tricky.
for (int start = 0; start <= sInput.Length - length; start++)
{
string sSubString = sInput.Substring(start, length);
Console.WriteLine(sSubString);
}
}
Console.ReadKey();
}
}

Output:

substrings

View More:

Conclusion:

I hope you loved this post. Please don’t hesitate to comment for any technical help. Your feedback and suggestions are welcome to me.

Thank You.

Leave a Comment