string - contains() method without prefix and suffix in Java -
i'm stuck on (simple, think) string
validation. use following method text edittext
can have 420 chars , must contain specific word (whatever place):
if(edittext.gettext().tostring().tolowercase().contains(stringtohave)) { } // stringtohave = specific string
however, want improve condition. example, have this:
string = "this not metastackoverflow question"; string b = "stackoverflow";
and want know if a
contains b
regardless case sensitive, then, follows:
if(a.tolowercase().contains(b)) { }
this condition true because indeed a
contains stackoverflow
. however, a
doesn't contain exactly b
, there prefix meta
. , not same word.. tried find way on , on other java websites without result.
how can improve contains()
method find exact string
without prefix or suffix? should use method (as containsonly()
, tried seems undefined string, it's check if contains numeric/alphabetic/etc. chars)?
edit:
add "two spaces" verification attractive , ingenious. however, if specific text @ end of sentence , add .
not work, isn't it?
a = "this great. on stackoverflow." // false because have "."
you can use regex here. matcher
class has find()
method, searches pattern in string. work:
matcher matcher = pattern.compile("(?i)\\b" + pattern.quote(b) + "\\b").matcher(a); if (matcher.find()) { // contains }
(?i)
case-insensitive embedded flag. pattern.quote()
used escape regex meta-characters if in search string.
Comments
Post a Comment