我需要取一个包含特定单词的短语,如果它确实包含该单词,即使它是另一个单词的一部分,也要打印出整个单词。
我想怎么找到单词"apple"
,但是我想不出怎么找到单词"appletree"
。
到目前为止,我有一些代码可以找到单词apple并将其打印出来。
String phrase = "She's sitting under an appletree!";
if (phrase.contains("apple")) {
System.out.println("apple");
} else {
System.out.println("none");
}
如何打印"appletree"
发布于 2019-06-27 09:35:22
不使用正则表达式,您可以简单地将句子拆分成单词,遍历并检查它是否包含所需的单词-老式风格
String [] arr = phrase.split ("\\s+");
for (String word : arr) {
if (word.contains("apple")) return word;
}
https://stackoverflow.com/questions/56782239
复制相似问题