So how do you check if a string has a particular word in it?
So this is my code:
a.setOnClickListener(new View.OnClickListener() {
@Overri
It's been correctly pointed out above that finding a given word in a sentence is not the same as finding the charsequence, and can be done as follows if you don't want to mess around with regular expressions.
boolean checkWordExistence(String word, String sentence) {
if (sentence.contains(word)) {
int start = sentence.indexOf(word);
int end = start + word.length();
boolean valid_left = ((start == 0) || (sentence.charAt(start - 1) == ' '));
boolean valid_right = ((end == sentence.length()) || (sentence.charAt(end) == ' '));
return valid_left && valid_right;
}
return false;
}
Output:
checkWordExistence("the", "the earth is our planet"); true
checkWordExistence("ear", "the earth is our planet"); false
checkWordExistence("earth", "the earth is our planet"); true
P.S Make sure you have filtered out any commas or full stops beforehand.
String sentence = "Check this answer and you can find the keyword with this code";
String search = "keyword";
Compare the line of string in given string
if ((sentence.toLowerCase().trim()).equals(search.toLowerCase().trim())) {
System.out.println("not found");
}
else {
System.out.println("I found the keyword");
}
if (someString.indexOf("Hey")>=0)
doSomething();
Solution-1: - If you want to search for a combination of characters or an independent word from a sentence.
String sentence = "In the Name of Allah, the Most Beneficent, the Most Merciful."
if (sentence.matches(".*Beneficent.*")) {return true;}
else{return false;}
Solution-2: - There is another possibility you want to search for an independent word from a sentence then Solution-1 will also return true if you searched a word exists in any other word. For example, If you will search cent from a sentence containing this word ** Beneficent** then Solution-1 will return true. For this remember to add space in your regular expression.
String sentence = "In the Name of Allah, the Most Beneficent, the Most Merciful."
if (sentence.matches(".* cent .*")) {return true;}
else{return false;}
Now in Solution-2 it wll return false because no independent cent word exist.
Additional: You can add or remove space on either side in 2nd solution according to your requirements.
Using contains
String sentence = "Check this answer and you can find the keyword with this code";
String search = "keyword";
if (sentence.toLowerCase().contains(search.toLowerCase())) {
System.out.println("I found the keyword..!");
} else {
System.out.println("not found..!");
}
You can use regular expressions:
if (d.matches(".*Hey.*")) {
c.setText("OUTPUT: SUCCESS!");
} else {
c.setText("OUTPUT: FAIL!");
}
.*
-> 0 or more of any characters
Hey
-> The string you want
If you will be checking this often, it is better to compile the regular expression in a Pattern
object and reuse the Pattern
instance to do the checking.
private static final Pattern HEYPATTERN = Pattern.compile(".*Hey.*");
[...]
if (HEYPATTERN.matcher(d).matches()) {
c.setText("OUTPUT: SUCCESS!");
} else {
c.setText("OUTPUT: FAIL!");
}
Just note this will also match "Heyburg"
for example since you didn't specify you're searching for "Hey"
as an independent word. If you only want to match Hey
as a word, you need to change the regex to .*\\bHey\\b.*