
使用 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); } } }}
输出
xyzxyyz
以上就是C# 程序查找字符串中的所有子字符串的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1435310.html
微信扫一扫
支付宝扫一扫