
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C# Program to Find All Substrings in a String
Use the substring() method in C# to find all substrings in a string.
Let’s say our string is −
Xyz
Loop through the length of the string and use the Substring function from the beginning to the end of the string −
for (int start = 0; start <= str.Length - i; start++) { string substr = str.Substring(start, i); Console.WriteLine(substr); }
Example
The following is the C# program to find all substrings in a string.
using System; class Demo { static void Main() { string str = "xyz"; for (int i = 1; i < str.Length; i++) { for (int start = 0; start <= str.Length - i; start++) { string substr = str.Substring(start, i); Console.WriteLine(substr); } } } }
Output
x y z xy yz
Advertisements