簡體   English   中英

如何使用 C# 查找符號后面的字母是大寫還是另一個符號

[英]How can I find if a letter following a symbol is in uppercase OR is another symbol using C#

我是 C# 的初學者(以及一般的編碼),所以如果這個問題很愚蠢,我很抱歉。

我必須找出此字符串中每個 / 后面的字母是大寫還是另一個 /。

string root = @"C:/File1/File2/File3/file1.txt"

並顯示錯誤的字符及其索引:

if (Char.IsLower(root[c])) ;
{
Console.WriteLine("There is an error!");
Console.WriteLine(Char.IsLower(root[c]) + " is in lowercase!");
}

我試過Char.IsUpper()str.IndexOf()但我不能組合字符串和字符,我不知道如何找到被檢查的字符:

int at;
int startIndex = 0;
at = root.IndexOf("/", startIndex);
int c = at + 1;

if (Char.IsUpper(c));

要獲取字符串中給定索引處的字符,請使用字符串索引屬性:

char charAtCIndex = root[c];

所以你可以做

if(char.IsUpper(root[c]) || root[c]=='/')
{
    //Do your stuff
}

盡管檢查索引是否在字符串長度內是一個很好的做法,就像/是字符串的最后一個字符一樣,您將獲得索引超出范圍異常:

if(c < root.Length && (char.IsUpper(root[c]) || root[c]=='/'))
{
    //Do your stuff
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM