gpt4 book ai didi

java - 如何在字符串文本中搜索单词,这个单词在java中以 "."或 ","结尾

转载 作者:行者123 更新时间:2023-11-29 04:34:32 25 4
gpt4 key购买 nike

有人可以帮我写代码吗?如何在String文本中搜索单词,这个单词以“.”结尾或 ","在 java 中

我不想这样搜索来找到它

String word = "test.";

String wordSerch = "I trying to tasting the Artestem test.";

String word1 = "test,"; // here with ","

String word2 = "test."; // here with "."

String word3 = "test"; //here without

//after i make string array and etc...

if((wordSearch.equalsIgnoreCase(word1))||
(wordSearch.equalsIgnoreCase(word2))||
(wordSearh.equalsIgnoreCase(word3))) {
}

if (wordSearch.contains(gramer))
//it's not working because the word Artestem will contain test too, and I don't need it

最佳答案

您可以对字符串使用 matches(Regex) 函数

String word = "test.";
boolean check = false;
if (word.matches("\w*[\.,\,]") {
check = true;
}

关于java - 如何在字符串文本中搜索单词,这个单词在java中以 "."或 ","结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42251188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com