Java-如何解析字符串中的特定单词

发布于 2024-08-22 13:07:07 字数 328 浏览 7 评论 0原文

我将如何解析“嗨,你好吗?”这句话中的“嗨”一词? 或者在“你好吗?”中解析“怎么样”这个词?

我想要的代码示例:

String word = "hi";
String word2 = "how";
Scanner scan = new Scanner(System.in).useDelimiter("\n");
String s = scan.nextLine();
if(s.equals(word)) {
System.out.println("Hey");
}
if(s.equals(word2)) {
System.out.println("Hey");
}

How would I parse for the word "hi" in the sentence "hi, how are you?"
or in parse for the word "how" in "how are you?"?

example of what I want in code:

String word = "hi";
String word2 = "how";
Scanner scan = new Scanner(System.in).useDelimiter("\n");
String s = scan.nextLine();
if(s.equals(word)) {
System.out.println("Hey");
}
if(s.equals(word2)) {
System.out.println("Hey");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

少女的英雄梦 2024-08-29 13:07:08

我会选择 tokenizer,相反。
将空格和其他元素(如逗号、句号等)设置为分隔符。并记住在不区分大小写的模式下进行比较。

这样,您就可以在“嗨,他的测试进行得怎么样”中找到“hi”,而不会在“his”上出现误报,在“Hi”上出现误报(以大写 H 开头)。

I'd go for a tokenizer, instead.
Set space and other elements like commas, full stops etc. as delimiters. And rememeber to compare in case-insensitive mode.

This way you can find "hi" in "Hi, how is his test going" without getting a false positive on "his" and a false negative on "Hi" (starts with a uppercase H).

囍笑 2024-08-29 13:07:08

您可以将正则表达式传递给 扫描仪。因此,您可以迭代输入中的每个单词(扫描仪默认情况下以空格分隔),并在获得匹配项时执行适当的处​​理。

You can pass a regular expression to the next() method of Scanner. So you can iterate through each word in the input (Scanner delimits on whitespace by default) and perform the appropriate processing if you get a match.

旧故 2024-08-29 13:07:07

要查找子字符串,您可以使用 containsindexOf 或任何其他变体:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

if( s.contains( word ) ) {
   // ...
}

if( s.indexOf( word2 ) >=0 ) {
   // ...
}

如果您关心字边界,那么 StringTokenizer 可能是一个好方法。

https://docs.oracle.com/ javase/1.5.0/docs/api/java/util/StringTokenizer.html

然后,您可以对每个单词执行不区分大小写的检查 (equalsIgnoreCase)。

To just find the substring, you can use contains or indexOf or any other variant:

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

if( s.contains( word ) ) {
   // ...
}

if( s.indexOf( word2 ) >=0 ) {
   // ...
}

If you care about word boundaries, then StringTokenizer is probably a good approach.

https://docs.oracle.com/javase/1.5.0/docs/api/java/util/StringTokenizer.html

You can then perform a case-insensitive check (equalsIgnoreCase) on each word.

作业与我同在 2024-08-29 13:07:07

看起来像是正则表达式的工作Contains 会给出一个误报,比如“hire-purchase”

if (Pattern.match("\\bhi\\b", stringToMatch)) { //...

Looks like a job for Regular Expressions. Contains would give a false positive on, say, "hire-purchase".

if (Pattern.match("\\bhi\\b", stringToMatch)) { //...
路弥 2024-08-29 13:07:07

我会选择java.util.StringTokenizerhttps://docs.oracle.com/javase/1.5.0/docs/api/java/util/StringTokenizer.html

StringTokenizer st = new StringTokenizer(
    "Hi, how are you?", 
    ",.:?! \t\n\r"       //whitespace and puntuation as delimiters
);
 while (st.hasMoreTokens()) {
     if(st.nextToken().equals("Hi")){
         //matches "Hi"
     }
 }

或者,看看 java .util.regex 并使用正则表达式。

I'd go for the java.util.StringTokenizer: https://docs.oracle.com/javase/1.5.0/docs/api/java/util/StringTokenizer.html

StringTokenizer st = new StringTokenizer(
    "Hi, how are you?", 
    ",.:?! \t\n\r"       //whitespace and puntuation as delimiters
);
 while (st.hasMoreTokens()) {
     if(st.nextToken().equals("Hi")){
         //matches "Hi"
     }
 }

Alternatively, take a look at java.util.regex and use regular expressions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文