检查字符串数组是否包含一个值,如果是,获取它的位置 [英] Checking if a string array contains a value, and if so, getting its position

查看:40
本文介绍了检查字符串数组是否包含一个值,如果是,获取它的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串数组:

string[] stringArray = { "text1", "text2", "text3", "text4" };
string value = "text3";

我想确定 stringArray 是否包含 value.如果是这样,我想定位它在数组中的位置.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆