Checking if a string array contains a value, and if so, getting its position

后端 未结 12 1131
日久生厌
日久生厌 2020-11-28 05:26

I have this string array:

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

I would like to

12条回答
  •  鱼传尺愫
    2020-11-28 06:02

    You could use the Array.IndexOf method:

    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
    }
    

提交回复
热议问题