使用 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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号