Here is one way to find all possible substrings of a given string in C#:
string inputString = "abcd"; int inputStringLength = inputString.Length; // Loop through each character in the input string for (int i = 0; i < inputStringLength; i++) { // Loop through each character after the current character for (int j = i + 1; j <= inputStringLength; j++) { // Get the substring starting at i and ending at j string substring = inputString.Substring(i, j - i); Console.WriteLine(substring); } }
This code uses nested loops to iterate through all possible combinations of substrings in the input string. The outer loop starts at the first character of the string and goes up to the second to last character, while the inner loop starts at the next character and goes up to the end of the string. The Substring
method is used to extract the substring between the current positions of the outer and inner loops.
For the input string “abcd“, the output of this code would be:
a ab abc abcd b bc bcd c cd d
Note that this code will also generate empty substrings (e.g., if the input string is an empty string or if the outer loop starts at the last character). You can add a check for non-empty substrings if necessary.