[英]How can I find if a letter following a symbol is in uppercase OR is another symbol using C#
I'm a beginner in C# (and in coding in general), so I'm sorry if this question is silly.我是 C# 的初学者(以及一般的编码),所以如果这个问题很愚蠢,我很抱歉。
I have to find if a letter following every / in this string is either an uppercase or another /.我必须找出此字符串中每个 / 后面的字母是大写还是另一个 /。
string root = @"C:/File1/File2/File3/file1.txt"
And display the wrong character and its index:并显示错误的字符及其索引:
if (Char.IsLower(root[c])) ;
{
Console.WriteLine("There is an error!");
Console.WriteLine(Char.IsLower(root[c]) + " is in lowercase!");
}
I tried with Char.IsUpper()
and str.IndexOf()
but I can't combine the strings and chars, and I don't know how to find the examinated character:我试过Char.IsUpper()
和str.IndexOf()
但我不能组合字符串和字符,我不知道如何找到被检查的字符:
int at;
int startIndex = 0;
at = root.IndexOf("/", startIndex);
int c = at + 1;
if (Char.IsUpper(c));
To get the char at a given index in a string, you use the string index property:要获取字符串中给定索引处的字符,请使用字符串索引属性:
char charAtCIndex = root[c];
So you can do所以你可以做
if(char.IsUpper(root[c]) || root[c]=='/')
{
//Do your stuff
}
It is a good practise though to check if the index if within the string length, as if the /
is the last character of the string you'll get an Index out of range exception:尽管检查索引是否在字符串长度内是一个很好的做法,就像/
是字符串的最后一个字符一样,您将获得索引超出范围异常:
if(c < root.Length && (char.IsUpper(root[c]) || root[c]=='/'))
{
//Do your stuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.