i’m trying to check a String file in java for a specific word.
example:
public String test() {
String text= "Your, me, everybody";
if(!text.contains("You"))
test1+= " and all!";
return test1;
}
edit: Thanks for replies. But what I need is this: I have a text full of words and commas. And if and ONLY if the word text.contains(you) or text.matches, if that word is not in the String, then it will be added. But if I try to do the example over, it says that “You” is in the text, and therefore wont do the test1+= ” and all!”;
In this particular example its not that dangerous, but when I have a string of about 100 words, there are some that are very alike. Or starts with the same letters or have a comma behind them.
Just use
text.contains("You")– String.contains() takes a CharSequence as a parameter. No need for regular expressions.Note that this is if the charSequence is in the string at all, so matches “Your” for, example. Quick and dirty for this particular demonstration – but I don’t suggest it (unless you WANT that behavior).
You’ll need to use String.matches(String regex) for the appropriate regex – as detailed in other answers.