简体   繁体   English

在Java字符串中查找单词位置

[英]Finding word position in a string for java

I have looked around for another answer on here, but i didn't really understand how to convert it to my own code. 我在这里寻找了另一个答案,但是我并不真正了解如何将其转换为自己的代码。 I am trying to find where "COUNTRY" is found in a phrase so: 我试图找到在短语中找到“ COUNTRY”的位置,以便:

String word = "COUNTRY";
String sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOU COUNTRY";

It should output that the word is in the 5th and 17th positions. 它应该输出该单词在第5位和第17位。

Many Thanks 非常感谢

EDIT: I realised that it also need to ignore the case. 编辑:我意识到它也需要忽略这种情况。 Sorry for not saying this sooner 抱歉,您不早点说

You can use this piece of code: 您可以使用以下代码:

public static void main(String args[]) {
    String word = "COUNTRY";
    String sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOU COUNTRY";
    String[] listWord = sentence.split(" ");
    for (int i = 0; i < listWord.length; i++) {
        if (listWord[i].equals(word)) {
            System.out.println(i+1);
        }
    }
}

If you only want to check if a String contains a Word then you can use the .contains(""); 如果只想检查字符串是否包含单词,则可以使用.contains(""); method provided by String String提供的方法

String word = "COUNTRY";
String sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOU COUNTRY";

System.out.println(sentence.contains(word));
//will return true;

If you want to find all the words inside the sentence then use this: 如果要查找句子中的所有单词,请使用以下命令:

String word = "COUNTRY";
String sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOU COUNTRY";

if (sentence.contains(word)) {
    String[] sentenceWords = sentence.split(" ");

    for (String wordInSentence : sentenceWords) {
        if (wordInSentence.equals(word)) {
            System.out.println(wordInSentence);
        }
    }
}

Or if you want to know the exact location of a specific word, then try this: 或者,如果您想知道特定单词的确切位置,请尝试以下操作:

String word = "COUNTRY";
String sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOU COUNTRY";

if (sentence.contains(word)) {
    String[] sentenceWords = sentence.split(" ");

    for (int i = 0; i < sentenceWords.length; i++) {
        if (sentenceWords[i].equals(word)) {
            System.out.println(word + " is located as the: " + i + "th string");
        }
    }
}

Note: See that i use .equals(); 注意:看到我使用.equals(); on String objects see this post for more information! 有关String对象的更多信息,请参见这篇文章!

EDIT: 编辑:

To ignore the case you can use String.equalsIgnoreCase() instead of String.equals() 要忽略这种情况,可以使用String.equalsIgnoreCase()代替String.equals()

Better use this functions present in java.lang.*,ie in String class.It is non Static... 最好使用java.lang。*(即String类)中存在的此函数。它是非静态的...

int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. int indexOf(String str)返回第一次出现的指定子字符串在此字符串中的索引。 int indexOf(String str, int fromIndex) Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. int indexOf(String str,int fromIndex)从指定的索引开始,返回指定子字符串首次出现在此字符串中的索引。

String s1="Hello I love my country,i belong to my country",s2="country";
System.out.println( s1.indexOf(s2));

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

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