C#: finding instances of a string within a string

前端 未结 5 1416
心在旅途
心在旅途 2021-01-28 01:28

Suppose I had the string \"1 AND 2 AND 3 OR 4\", and want to create an array of strings that contains all substrings \"AND\" or \"OR\", in order, found within the string.

<
5条回答
  •  旧时难觅i
    2021-01-28 01:51

    Here's a goofy way that I came up with:

    string rule = "1 AND 2 AND 3 OR 4";
    List andsOrs = new List();
    string[] split = rule.Split();
    for (int i = 0; i < split.Length; i++)
    {
       if (split[i] == "AND" || split[i] == "OR")
       {
           andsOrs.Add(split[i]);
       }
    }
    string[] conditions = andsOrs.ToArray();
    return conditions;
    

提交回复
热议问题