【发布时间】:2014-02-07 22:15:54
【问题描述】:
我想检查输入的特定单词是否在字符串中。我使用了以下代码:
import java.util.*;
import java.io.*;
public class StringTest {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
String s1;
s1 = s.next();
String s2 = "this is an accomplishment";
if (s2.contains(s1)) {
System.out.println("you have done it");
} else
System.out.println("try another word");
}
}
如果我输入单词“com”,我会得到结果
你已经做到了
但“com”实际上只是“成就”一词的一部分。我希望这种情况下的结果是
换个词
因为s2 不包含完整的单词“com”。我也试过indexOf(),但这也给出了相同的结果。我应该怎么做?
【问题讨论】: