How to print all substrings of a given string in C#

1 Answer

0 votes
using System;
using System.Text;
 
class Program
{
    static void Main() {
        string s = "abcd";
            
        for (int i = 0; i < s.Length; i++) {
            StringBuilder sb = new StringBuilder(s.Length - i);
            for (int j = i; j < s.Length; ++j) {
                sb.Append(s[j]);
                Console.WriteLine(sb);
            }
        }
    }
}
 
 
 
 
/*
run:
 
a
ab
abc
abcd
b
bc
bcd
c
cd
d
 
*/

 



answered Sep 5, 2021 by avibootz
edited Jul 19, 2022 by avibootz
...