简体   繁体   English

如何在句子中搜索特定单词

[英]How to search for a particular word in a sentence

I am actually a newbie to Java and was trying to do a small project.我实际上是 Java 的新手,并且正在尝试做一个小项目。 So, in my project, I want to allow the user to enter a sentence, and I want the program to search for particular words in the sentence and give outputs based on that.因此,在我的项目中,我希望允许用户输入一个句子,并且我希望程序搜索句子中的特定单词并根据它给出输出。 I use NetBeans to develop my applications.我使用 NetBeans 来开发我的应用程序。

My code is something like this我的代码是这样的

String Sentence=jTextField1.getText();
if (Sentence.equals("Hello")
{
jTextField2.setText("Hello was found");
}
else if (Sentence.equals("Donkey")
{
jTextField2.setText("Donkey was found");
}

I know that this code makes no sense and wont run, but I put it so that people can get a general idea of what I am trying to achieve.我知道这段代码没有意义并且不会运行,但我把它放出来是为了让人们可以大致了解我想要实现的目标。

Please help me.请帮我。

Use .contains like so:像这样使用.contains

String Sentence = jTextField1.getText();
if (Sentence.contains("Hello")) {
    jTextField2.setText("Hello was found");
} else if (Sentence.contains("Donkey")) {
    jTextField2.setText("Donkey was found");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM