首页 > 解决方案 > 如何使用 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));

标签: 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
}

推荐阅读