开发者社区> 问答> 正文

检查字符串数组是否包含值,如果是,则获取其位置

我有这个字符串数组:

string[] stringArray = { "text1", "text2", "text3", "text4" }; string value = "text3"; 我想确定是否stringArray包含value。如果是这样,我想在数组中找到它的位置。

我不想使用循环。谁能建议我该怎么做?

展开
收起
保持可爱mmm 2020-02-06 23:19:48 350 0
1 条回答
写回答
取消 提交回答
  • 您可以使用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 }

    问题来源于stack overflow

    2020-02-06 23:20:02
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
重新定义计算的边界 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载