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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP