C#程序查找字符串中的所有子字符串
使用substring()C#中的方法查找字符串中的所有子字符串。
假设我们的字符串是-
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
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短