Java 检查字符串是否包含特定单词
那么如何检查字符串中是否有特定的单词呢 这是我的代码:Java 检查字符串是否包含特定单词,java,android,Java,Android,那么如何检查字符串中是否有特定的单词呢 这是我的代码: a.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(d.contains("Hey")){ c.setText("OUT
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(d.contains("Hey")){
c.setText("OUTPUT: SUCCESS!");
}else{
c.setText("OUTPUT: FAIL!");
}
}
});
我收到一个错误。您可以使用正则表达式:
if (d.matches(".*Hey.*")) {
c.setText("OUTPUT: SUCCESS!");
} else {
c.setText("OUTPUT: FAIL!");
}
*
->任何字符中的0个或多个
嘿
->你想要的字符串
如果要经常检查,最好在模式
对象中编译正则表达式,并重用模式
实例进行检查
private static final Pattern HEYPATTERN = Pattern.compile(".*Hey.*");
[...]
if (HEYPATTERN.matcher(d).matches()) {
c.setText("OUTPUT: SUCCESS!");
} else {
c.setText("OUTPUT: FAIL!");
}
只需注意,这也将匹配“Heyburg”
,例如,因为您没有指定将搜索“Hey”
作为独立词。如果只想将Hey
作为单词进行匹配,则需要将正则表达式更改为*\\bHey\\b.
.contains()
是完全有效的检查方法
()
由于您没有发布错误,我猜d
要么为null,要么您得到了“无法引用在不同方法中定义的内部类中的非最终变量”错误
为了确保它不是null,首先检查if语句中的null。如果是另一个错误,请确保将
d
声明为final
或是类的成员变量。对于c
也一样,没有他们说的那么复杂,检查一下你不会后悔的。
if (someString.indexOf("Hey")>=0)
doSomething();
String sentence = "Check this answer and you can find the keyword with this code";
String search = "keyword";
if ( sentence.toLowerCase().indexOf(search.toLowerCase()) != -1 ) {
System.out.println("I found the keyword");
} else {
System.out.println("not found");
}
如果需要,您可以更改
toLowerCase()
。使用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..!");
}
比较给定字符串中的字符串行
if ((sentence.toLowerCase().trim()).equals(search.toLowerCase().trim())) {
System.out.println("not found");
}
else {
System.out.println("I found the keyword");
}
也许这篇文章很旧,但我偶然发现它并使用了“错误”的用法。查找关键字的最佳方法是使用
。包含,例如:
if ( d.contains("hello")) {
System.out.println("I found the keyword");
}
上面已经正确地指出,在一个句子中查找给定的单词与查找字符序列不同,如果您不想混淆正则表达式,可以按如下方法进行操作
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;
}
输出:
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
确保事先过滤掉任何逗号或句号。解决方案-1:-如果要从句子中搜索字符组合或独立单词
String sentence = "In the Name of Allah, the Most Beneficent, the Most Merciful."
if (sentence.matches(".*Beneficent.*")) {return true;}
else{return false;}
解决方案-2:-还有一种可能,您希望从一个句子中搜索一个独立的单词,如果您搜索的单词存在于任何其他单词中,解决方案-1也将返回true。例如,如果您将从包含单词**慈善**的句子中搜索cent,则Solution-1将返回true。为此,请记住在正则表达式中添加空格
String sentence = "In the Name of Allah, the Most Beneficent, the Most Merciful."
if (sentence.matches(".* cent .*")) {return true;}
else{return false;}
现在在解决方案-2中,它将返回false,因为不存在独立的cent单词
附加:您可以根据需要在第二个解决方案的任一侧添加或删除空间 另一个答案(到目前为止)似乎是检查子字符串而不是单词。主要区别
在的帮助下,我创建了以下简单方法:
static boolean containsWord(String mainString, String word) {
Pattern pattern = Pattern.compile("\\b" + word + "\\b", Pattern.CASE_INSENSITIVE); // "\\b" represents any word boundary.
Matcher matcher = pattern.matcher(mainString);
return matcher.find();
}
阅读有关正则表达式(RegEx)的更多信息。检查部分似乎很好。怎么了?怎么了?什么是d
?什么是c
?哦,我的错。我忘记了d=b.getText().toString();部分。我猜你是想写这篇评论作为对你问题的评论。@m0skit0我们可以忽略大小写来进行匹配吗,即,我想匹配一个包含android/android的句子,它应该返回true。@KaranKhurana取决于OP实际想要什么或“word”的意思。@m0skit0 OP是什么?谢谢你的回答。这是一个老问题,但我今天在工作中遇到了类似的情况。上面的代码搜索的是字符序列,而不是单词。例如:-如果我们只想捕获“关键字”,而不想捕获任何像“关键字1”这样的扩展,那么上面的代码将不起作用。避免这种情况的一种方法是使用String contains函数搜索“关键字”(注意两边的两个空格)——有没有更优雅的方法呢?@JavaTec您需要使用正则表达式来正确搜索单词。与m0skit0的答案类似(如果您反复使用同一个表达式,请参阅使用编译表达式的答案),您需要执行以下操作:if(d.matches(\\bkeyword\\b”){/*success*/}
\b
(\\b
以字符串形式转义反斜杠)匹配“单词边界”,即单词的边缘。与使用空格相比,它的优势在于它可以正确地匹配“带关键字的句子”(句号而不是空格)甚至“关键字”(开头或结尾没有空格)。谢谢@loganpick-我确实使用了\\b
(10月份时)并且效果很好。此代码只检查字符串是否包含其他字符串。语义不适用于实际单词。奶酪!=奶酪汉堡和猫!=对不起。我使用了contains而不是indexOfLooks,这与您的另一个答案基本相同。请不要转载;而是编辑你的旧答案,让它变得更好。它被否决的原因可能是因为它不正确(尝试将关键字更改为asdf
,并让代码报告找到它)。您确定这会起作用吗?看看Bencent后面是逗号而不是空格。
static boolean containsWord(String mainString, String word) {
Pattern pattern = Pattern.compile("\\b" + word + "\\b", Pattern.CASE_INSENSITIVE); // "\\b" represents any word boundary.
Matcher matcher = pattern.matcher(mainString);
return matcher.find();
}