C#中的子字符串
Substring用于在C#中获取字符串的子部分。我们有用substring()于此目的的方法。使用substring()C#中的方法检查每个子字符串的唯一字符。循环直到字符串的长度。
如果子字符串中的任何人与另一个匹配,则表示该字符串没有唯一字符。
您可以尝试运行以下代码来确定字符串是否包含所有唯一字符。该示例显示了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();
}
}