Perform Trim() while using Split()

后端 未结 7 1201
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-23 15:53

today I was wondering if there is a better solution perform the following code sample.

string keyword = \" abc, foo  ,     bar\";
string match = \"foo\";
str         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 16:44

    Another possible option (that avoids LINQ, for better or worse):

    string line = " abc, foo  ,     bar";
    string[] parts= Array.ConvertAll(line.Split(','), p => p.Trim());
    

    However, if you just need to know if it is there - perhaps short-circuit?

    bool contains = line.Split(',').Any(p => p.Trim() == match);
    

提交回复
热议问题