开发者

check a string if have a word

I have a string li开发者_Python百科ke this: "TEST.DATA.Data.COR.Point,2;TEST.DATA.Data.COR.Point,5;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.WordTOFIND,18"

I have a list of array with that, but some dont have that wordtofind.

My question is - how can I compare the string to check if have that word?


bool contains = str.ToLower().Contains("wordtofind") i think...


Something like this would probably do:

string input = "TEST.DATA.Data.COR.Point,2;TEST.DATA.Data.COR.Point,5;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.WordTOFIND,18";
bool stringContainsWord = input.IndexOf("wordtofind", 
                                        StringComparison.OrdinalIgnoreCase) >= 0;


Do a loop through your array and test if each element/string contains the value.

String input = "TEST.DATA.Data.COR.Point,2;TEST.DATA.Data.COR.Point,5;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.WordTOFIND,18";
if ( input.Contains("WordTOFIND") == true)
{
//your code
}


You can use the string.Contains method.

bool containsWord = "[...]WordTOFIND[...]".Contains("WordTOFIND");

For example, if you are trying to find the elements in your list that contain "WordTOFIND" you could do:

IList<string> myList = ...

var result = myList.Where(s=>s.Contains("WordTOFIND"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜