Darrkwolf
Darrkwolf

Reputation: 3

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:

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.

Many Thanks

EDIT: I realised that it also need to ignore the case. Sorry for not saying this sooner

Upvotes: 0

Views: 4214

Answers (3)

Panu
Panu

Reputation: 11

Better use this functions present in java.lang.*,ie in String class.It is non Static...

int indexOf(String str) Returns the index within this string of the first occurrence of the specified substring. 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.

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

Upvotes: 1

Igoranze
Igoranze

Reputation: 1486

If you only want to check if a String contains a Word then you can use the .contains(""); method provided by 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(); on String objects see this post for more information!

EDIT:

To ignore the case you can use String.equalsIgnoreCase() instead of String.equals()

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

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);
        }
    }
}

Upvotes: 1

Related Questions