C# program to find all substrings in a string

WBOY
Release: 2023-09-09 18:57:02
forward
795 people have browsed it

C# 程序查找字符串中的所有子字符串

Find all substrings in a string using the substring() method in C#.

Suppose our string is -

Xyz
Copy after login

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);
}
Copy after login

Example

The following is a C# program to find all substrings in a string.

Live Demo

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);
         }
      }
   }
}
Copy after login

Output

x
y
z
xy
yz
Copy after login

The above is the detailed content of C# program to find all substrings in a string. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!