Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to List all Substrings in a given String using C#?
A substring is any contiguous sequence of characters within a string. In C#, you can generate all possible substrings of a given string using the Substring() method with nested loops to iterate through different starting positions and lengths.
Syntax
The Substring() method extracts a portion of a string −
string.Substring(startIndex, length)
To generate all substrings, use nested loops −
for (int length = 1; length <= str.Length; length++) {
for (int start = 0; start <= str.Length - length; start++) {
string substring = str.Substring(start, length);
}
}
How It Works
The algorithm uses two nested loops: the outer loop controls the length of substrings (from 1 to the full string length), while the inner loop controls the starting position for each substring of that length.
Using Nested Loops
Example
using System;
public class Demo {
public static void Main() {
string myStr = "pqrz";
Console.WriteLine("All substrings of "" + myStr + "":");
for (int length = 1; length <= myStr.Length; length++) {
for (int start = 0; start <= myStr.Length - length; start++) {
string substr = myStr.Substring(start, length);
Console.WriteLine(substr);
}
}
}
}
The output of the above code is −
All substrings of "pqrz": p q r z pq qr rz pqr qrz pqrz
Using a Method to Generate Substrings
Example
using System;
using System.Collections.Generic;
public class SubstringGenerator {
public static List<string> GetAllSubstrings(string input) {
List<string> substrings = new List<string>();
for (int length = 1; length <= input.Length; length++) {
for (int start = 0; start <= input.Length - length; start++) {
substrings.Add(input.Substring(start, length));
}
}
return substrings;
}
public static void Main() {
string text = "ABC";
List<string> allSubstrings = GetAllSubstrings(text);
Console.WriteLine("String: " + text);
Console.WriteLine("Total substrings: " + allSubstrings.Count);
Console.WriteLine("Substrings:");
foreach (string substring in allSubstrings) {
Console.WriteLine(""" + substring + """);
}
}
}
The output of the above code is −
String: ABC Total substrings: 6 Substrings: "A" "B" "C" "AB" "BC" "ABC"
Performance Considerations
For a string of length n, the total number of possible substrings is n × (n + 1) / 2. This means the time complexity is O(n²) for generating all substrings, and space complexity depends on whether you store them or just process them.
Example with Count
using System;
public class SubstringCounter {
public static void Main() {
string[] testStrings = {"A", "AB", "ABC", "ABCD"};
foreach (string str in testStrings) {
int count = str.Length * (str.Length + 1) / 2;
Console.WriteLine("String: "" + str + "" - Length: " + str.Length + " - Substrings: " + count);
}
}
}
The output of the above code is −
String: "A" - Length: 1 - Substrings: 1 String: "AB" - Length: 2 - Substrings: 3 String: "ABC" - Length: 3 - Substrings: 6 String: "ABCD" - Length: 4 - Substrings: 10
Conclusion
Generating all substrings in C# requires nested loops with the Substring() method. The outer loop controls substring length while the inner loop controls the starting position. For a string of length n, this produces n×(n+1)/2 total substrings with O(n²) time complexity.
