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

WBOY
WBOY 转载
2023-09-09 18:57:02 406浏览

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

使用 C# 中的 substring() 方法查找字符串中的所有子字符串。

假设我们的字符串是 -

Xyz

循环遍历字符串的长度并从字符串的开头到结尾使用 Substring 函数 -

for (int start = 0; start <= str.Length - i; start++) {
   string substr = str.Substring(start, i);
   Console.WriteLine(substr);
}

示例

以下是查找字符串中所有子字符串的 C# 程序。

现场演示

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);
         }
      }
   }
}

输出

x
y
z
xy
yz

以上就是C# 程序查找字符串中的所有子字符串的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除