检查是否一个字符串数组包含一个值,如果是这样,获取其位置 [英] Checking if a string array contains a value, and if so, getting its position
本文介绍了检查是否一个字符串数组包含一个值,如果是这样,获取其位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的字符串数组:
I have this string array:
string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
我想,以确定是否字符串数组
包含值
。如果是这样,我想找到其在数组中的位置。
I would like to determine if stringArray
contains value
. If so, I want to locate its position in the array.
我不想使用循环。任何人都可以建议我怎么可以这样做?
I don't want to use loops. Can anyone suggest how I might do this?
推荐答案
您可以使用的 Array.IndexOf 方法
string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";
int pos = Array.IndexOf(stringArray, value);
if (pos > -1)
{
// the array contains the string and the pos variable
// will have its position in the array
}
这篇关于检查是否一个字符串数组包含一个值,如果是这样,获取其位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文