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"));
精彩评论