在 C# 中取子字符串
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();
}
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP