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

后端 未结 12 1084
日久生厌
日久生厌 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:23

    string x ="Hi ,World";
    string y = x;
    char[] whitespace = new char[]{ ' ',\t'};          
    string[] fooArray = y.Split(whitespace);  // now you have an array of 3 strings
    y = String.Join(" ", fooArray);
    string[] target = { "Hi", "World", "VW_Slep" };
    
    for (int i = 0; i < target.Length; i++)
    {
        string v = target[i];
        string results = Array.Find(fooArray, element => element.StartsWith(v, StringComparison.Ordinal));
        //
        if (results != null)
        { MessageBox.Show(results); }
    
    }
    

提交回复
热议问题