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 送给老师中秋祝福语简短
18 写给情侣祝福语 浪漫简短