c# - 如何使用 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
}
推荐阅读
- angularjs - 如何在 AngularJS 中将 ng-model 变量重置为 Null?
- java - 使用 Java 代码中的不同引号执行 Bash one-liner 以进行 Elasticsearch 查询
- r - 在文本分析中提取正确的文本(瑞典语)
- java - 尝试通过命令行触发自动化测试,但看到“java.lang.ClassNotFoundException:org.testng.TestNG”
- elasticsearch - 如何调整小索引的 translog 保留?
- reactjs - 使用 react 和 redux 为 post 请求创建等待加载程序警报
- r - 如何在 R 中实现这个 for 循环?
- json - 无法映射响应 json 数组
- python - 我们可以在 Mac 上拥有多个张量流版本吗?
- mongodb - 如何在mongodb中使用不同的聚合