Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查找字符串元素的位置_C#_Arrays_String - Fatal编程技术网

C# 查找字符串元素的位置

C# 查找字符串元素的位置,c#,arrays,string,C#,Arrays,String,需要一些帮助来查找字符串中的特定字母。 我需要在字符串数组和输出中找到字母“aeiou”,以获得第一个找到的字母的位置。 一切都在C#中 我哪里出错了 string array = "Elephants are dangerous"; char[] letters = ("aeiou").ToCharArray(); // make char array to iterate through all characters. you could make this also "inline" in

需要一些帮助来查找字符串中的特定字母。 我需要在字符串数组和输出中找到字母“aeiou”,以获得第一个找到的字母的位置。 一切都在C#中

我哪里出错了

string array = "Elephants are dangerous";
char[] letters = ("aeiou").ToCharArray(); // make char array to iterate through all characters. you could make this also "inline" in the foreach i just left it her so you see what's going on.

int firstIndex = int.MaxValue;
char firstletter = '?';

foreach (char letter in letters) // iterate through all charecters you're searching for
{
    int index = array
        .ToLower() // to lower -> remove this line if you want to have case sensitive search
        .IndexOf(letter); // get the index of the current letter

    //check if the character is found and if it's the earliest position
    if (index != -1 && index < firstIndex ) 
    {
        firstIndex = index;
        firstletter = letter;
    }
}
Console.WriteLine("Letter: {0} @ {1}", firstletter, firstIndex);
EDIT2这里是一个正则表达式

string array = "Elephants are dangerous";
Match match = Regex.Match(array.ToLower(), "[aeiou]");

if (match.Success)
{
    Console.WriteLine("Letter: {0} @ {1}", match.Value, match.Index);
}

EDIT2这里是一个正则表达式

string array = "Elephants are dangerous";
Match match = Regex.Match(array.ToLower(), "[aeiou]");

if (match.Success)
{
    Console.WriteLine("Letter: {0} @ {1}", match.Value, match.Index);
}

与任何循环解决方案相比,我更喜欢这种方法。这是简洁的,显然是正确的,并且在面对不断变化的需求时是可维护的



与任何循环解决方案相比,我更喜欢这种方法。在面对不断变化的需求时,这是简明的、明显正确的和可维护的。

我不确定我是否理解您要做的事情。您想删除过滤器中未包含的所有字母的原始字符串吗?在您的情况下,您希望
大象
中的
E
为零,对吗?在这种情况下-是的,我想得到零。
大象是危险的
不包含
I
.All
的结果检查是否为false。如果(letters.ToLower().All(p=>array.ToLower().Contains(p)),它也应该不区分大小写-
if(letters.ToLower().All(p=>array.ToLower().Contains(p))
我不确定我是否理解您的意图。您想删除过滤器中未包含的所有字母的原始字符串吗?在您的情况下,您希望
大象
中的
E
为零,对吗?在这种情况下-是的,我想得到零。
大象是危险的
不包含
I
.All
的结果检查是否为false。如果(letters.ToLower().All(p=>array.ToLower().Contains(p)),它也应该不区分大小写-
if(letters.ToLower().All(p=>array.ToLower().Contains(p))
nice!但为什么要强制转换为可为null的int?因为可能存在零匹配,我们不想使用神奇的int值@Matit花了我一些时间来理解可空的minIndex。真聪明!我想再投一票;-)@Mat这是一种更通用的模式,您可以在需要不可为null的值类型序列的第一项时使用它。所以一旦你知道了这个模式,你就会在很多地方认出它。这里是Min,而不是FirstOrDefault。很好!但为什么要强制转换为可为null的int?因为可能存在零匹配,我们不想使用神奇的int值@Matit花了我一些时间来理解可空的minIndex。真聪明!我想再投一票;-)@Mat这是一种更通用的模式,您可以在需要不可为null的值类型序列的第一项时使用它。所以一旦你知道了这个模式,你就会在很多地方认出它。在这里,它是Min而不是FirstOrDefault。
string array = "Elephants are dangerous";
Match match = Regex.Match(array.ToLower(), "[aeiou]");

if (match.Success)
{
    Console.WriteLine("Letter: {0} @ {1}", match.Value, match.Index);
}
int? minIndex =
 letters
 .Select(l => (int?)array.IndexOf(l))
 .Where(idx => idx != -1)
 .Min();