C# 数组查找包含指定字符的索引 - 使用 LINQ 的 IndexOf 方法
C# 数组查找包含指定字符的索引可以使用 LINQ 中的 IndexOf 方法。以下是示例代码:
string[] words = { 'apple', 'banana', 'cherry', 'date' };
char c = 'a';
int index = Array.FindIndex(words, w => w.IndexOf(c) >= 0);
if (index >= 0)
{
Console.WriteLine($'The first word containing '{c}' is '{words[index]}' at index {index}.');
}
else
{
Console.WriteLine($'No word contains '{c}'.');
}
此代码将在 words 数组中查找第一个包含字符 'a' 的单词,并输出该单词和索引。如果没有单词包含该字符,则输出相应的消息。
原文地址: https://www.cveoy.top/t/topic/oLbg 著作权归作者所有。请勿转载和采集!