
Substring 用于在 C# 中获取字符串的子部分。为此,我们有 substring() 方法。使用 C# 中的 substring() 方法检查每个子字符串是否有唯一字符。循环它直到字符串的长度。
如果子字符串中的任何一个与另一个子字符串匹配,则意味着该字符串没有唯一字符。
您可以尝试运行以下命令确定字符串是否包含所有唯一字符的代码。该示例显示了 Substring() 方法的用法 –
示例
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;public class Demo { public bool CheckUnique(string str) { string one = ""; string two = ""; for (int i = 0; i < str.Length; i++) { one = str.Substring(i, 1); for (int j = 0; j < str.Length; j++) { two = str.Substring(j, 1); if ((one == two) && (i != j)) return false; } } return true; } static void Main(string[] args) { Demo d = new Demo(); bool b = d.CheckUnique("amit"); Console.WriteLine(b); Console.ReadKey(); }}
以上就是C# 中的子字符串的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1435481.html
微信扫一扫
支付宝扫一扫