C# 中的子字串
子字串用於獲取 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();
}
}
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP