Better way to find value in comma separated string (Java/Android) -


assuming string defined:

string list = "apples,orange,bears,1,100,20,apple"; 

without separating list out collection or array, there better way find string in list? instance, if search "bear", there should no result, since there no exact match (bears doesn't count). can't ",bear," since there's no guarantee word bear not appear @ beginning or end of file.

you still use like:

(^|,)bears(,|$) 

it commas, or beginning or end of line, believe you're looking for.

edit: addendum

since denomales mentioned it, above regex search consume commas (if any) can find, overlapping matches in case of lists apples,orange,bears,honey,bears,bears,bears,bears,9999,bees , count 3 bears out of 5 present. can in case use lookarounds. might take bit head around them, gist of behind, or ahead of characters without consuming them. makes possible find 5 bears, , how use them:

(?<=^|,)bears(?=,|$) 

a breakdown of characters...

^ means beginning of line, in case there no commas, still match.

, literal comma.

(?<= ... ) positive lookbehind , behind b character in bears make sure there what's inside, either ^ or ,.

(?= ... ) positive lookahead , after s character in bears make sure there what's inside, either , or $.

$ means end of line, working ^, @ end.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -