c# - 如何检查数组字符串的最后一个字符是否以“A”结尾
问题描述
我制作了一个数组,用户在其中输入了几个名称,然后我希望程序将它们打印出来。如果字母以“a”结尾,我希望它改变颜色。这就是我在代码中的意思。
Array.Sort(stodents);
Console.WriteLine("----------");
for (int i = 0; i < stodents.Length; i++)
{
if (What do I type here?)
{
Console.ForegroundColor = ConsoleColor.Magenta;
}
else
{
Console.ForegroundColor = ConsoleColor.Blue;
}
Console.WriteLine(stodents[i]);
}
所以是的,我希望它在字符串不以 A 结尾时使字符串变为蓝色,而当它以洋红色结尾时。
解决方案
您可以使用String.EndsWith方法。
if(stodents[i].EndsWith('a'))
该方法检查字符串是否以指定的字符/字符串结尾(取决于您使用的重载),如果找到匹配项则返回 true。
如果要使其不区分大小写检查,也可以将重载与StringComparison 枚举一起使用
例如,
if(stodents[i].EndsWith("a",StringComparison.CurrentCultureIgnoreCase))
推荐阅读
- android - 为什么 AndroidManifest.xml 的 targetSdkVersion 无效?
- wordpress - 从自定义帖子类型中删除标题并添加新按钮 - wordpress
- node.js - 如何在 mongodb 中创建“稀疏”多文档唯一索引?
- git - 如何删除本地 ssh 隧道
- visual-studio-code - 如何更改 VS Code 侧边栏中的字体大小?
- javascript - 工具提示 html 谷歌图表
- javascript - 没有在标题中获取参数
- python - Python3 typehints - 传入类型元组后,指定返回值是这些类型实例的元组
- javascript - 如何在socket.io中请求url
- swift - swift中的WKwebview