C# - Different Ways to Find All Substrings in a String
Last Updated :
21 Oct, 2021
Given a string as an input we need to find all the substrings present in the given string.
Example:
Input:
geeks
Output:
g
e
e
k
s
ge
ee
ek
ks
gee
eek
eks
geek
eeks
geeks
Input:
ab
Output:
a
b
ab
Method 1: Using Substring() method
We can find all the substrings from the given string using the Substring() method. This method returns a substring from the current string. It contains two parameters where the first parameter represents the starting position of the substring which has to be retrieved and the second parameter will represent the length of the substring. Here if the first parameter is equal to the length of the string then this method will return nothing.
Syntax:
str.Substring(strindex, strlen)
where strindex is the starting index of the substring and strlen is the length of the substring.
Approach
To display
- Read the string from the user.
- Write the find_substrings() function to get substrings.
- In find_substrings() function call Substring() method to get the substrings.
for(i = 1; i <= input_string.Length; i++)
{
for (j = 0; j <= input_string.Length - i; j++)
{
// Use Substring function
Console.WriteLine(input_string.Substring(j, i));
}
}
- Now show the retrieved substrings.
Example:
C#
// C# program to display all Substrings
// present in the given String
using System;
class GFG{
// Function to get the substrings
static void find_substrings(string input_string)
{
int j = 0;
int i = 0;
for(i = 1; i <= input_string.Length; i++)
{
for(j = 0; j <= input_string.Length - i; j++)
{
// Using Substring() function
Console.WriteLine(input_string.Substring(j, i));
}
}
}
// Driver code
public static void Main()
{
// Declare the main string
string input_string;
Console.Write("Enter String : ");
Console.Write("\n");
// Read the string
input_string = Console.ReadLine();
// Call the function
find_substrings(input_string);
}
}
Output:
Enter String :
GFG
G
F
G
GF
FG
GFG
Method 2: Using for loop
We can also find substring from the given string using nested for loop. Here the outer for loop is used to select starting character, mid for loop is used to considers all characters on the right of the selected starting character as the ending character of the substring, and the inner for loop is used to print characters from the starting to the ending point.
Example:
C#
// C# program to display all Substrings
// present in the given String
using System;
class GFG{
// Function to print all substrings
// from the given string
static void find_Substring(string inputstr, int n)
{
// Choose starting point
for(int l = 1; l <= n; l++)
{
// Choose ending point
for(int i = 0; i <= n - l; i++)
{
// Display the substrings
int q = i + l - 1;
for(int j = i; j <= q; j++)
Console.Write(inputstr[j]);
Console.WriteLine();
}
}
}
// Driver code
static public void Main ()
{
string inputstr = "Geeks";
// Calling function
find_Substring(inputstr, inputstr.Length);
}
}
Output:
G
e
e
k
s
Ge
ee
ek
ks
Gee
eek
eks
Geek
eeks
Geeks
Similar Reads
Get a Substring in C A substring is a contiguous sequence of characters within a string. In this article, we will learn how to extract a substring using a C program.The simplest method to get a substring from a larger string is by using strncpy() function. Letâs take a look at an example:C++#include <stdio.h> #inc
2 min read
Shell Program to Find the Position of Substring in Given String A string is made of many substrings or can say that if we delete one or more characters from the beginning or end then the remaining string is called substring. This article is about to write a shell program that will tell the position (index) of the substring in a given string. Let's take an exampl
7 min read
String find() in C++ In C++, string find() is a built-in library function used to find the first occurrence of a substring in the given string. Letâs take a look at a simple example that shows the how to use this function:C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Welcome to GfG!"; s
4 min read
Substrings: A Comprehensive Guide Substrings are a fundamental concept in computer science and programming. They play a crucial role in various applications, from text manipulation to data processing. In this blog post, we'll explore what substrings are, their full form, use cases, examples, when to use them, when to avoid them, bes
8 min read
CSES Solutions - Substring Order II You are given a string of length n. If all of its substrings (not necessarily distinct) are ordered lexicographically, what is the kth smallest of them? Examples: Input: String = "agctagctagct" , k = 7Output: AGCExplanation: The 7 smallest substrings in order are a, a, a, ag, ag, ag and agc. Input:
15+ min read
String Tokenization in C In C, tokenization is the process of breaking the string into smaller parts using delimiters (characters treated as separators) like space, commas, a specific character, or even a string. Those smaller parts are called tokens where each token is a substring of the original string separated by the de
3 min read