Java String Index Of indexOfWord(String word, String string)

Here you can find the source of indexOfWord(String word, String string)

Description

returns the index (word count) of word in the string.

License

Open Source License

Declaration


public static int indexOfWord(String word, String string) 

Method Source Code

//package com.java2s;
/*/* w w w  .j a v a 2 s . c o m*/
 * Copyright: (c) 2002-2006 Mayo Foundation for Medical Education and
 * Research (MFMER).  All rights reserved.  MAYO, MAYO CLINIC, and the
 * triple-shield Mayo logo are trademarks and service marks of MFMER.
 *
 * Except as contained in the copyright notice above, the trade names, 
 * trademarks, service marks, or product names of the copyright holder shall
 * not be used in advertising, promotion or otherwise in connection with
 * this Software without prior written authorization of the copyright holder.
 * 
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 *       http://www.eclipse.org/legal/epl-v10.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.*;

public class Main {
    /**
    returns the index (word count) of word in the string.  If word is not in the string then it
    returns -1.  If word exists inside string that does not mean that it is considered a 'word'
    in the string.
    Uses StringTokenizer
    */

    public static int indexOfWord(String word, String string) {
        word = word.toLowerCase().trim();
        String wordInString;

        StringTokenizer st = new StringTokenizer(string);
        for (int i = 0; st.hasMoreElements(); i++) {
            wordInString = st.nextToken().toLowerCase().trim();
            if (word.equals(wordInString))
                return i;
        }

        return -1;
    }
}

Related

  1. indexOf(final String str, final String searchString)
  2. indexOfClosingBracket(String text, int openingBracket)
  3. indexOfIgnoreCase(final String src, char c, int startIndex, int endIndex)
  4. indexOfIgnoreCase(String str, String substring)
  5. indexOfMultiple(String line, char character, int count, int startIndex)
  6. isPhaseGroupContainsPhase(int index, String phaseName)
  7. lastIndexOf(String pattern, String s)
  8. lastIndexOfIgnoreCase(final String s, final String subS)
  9. mergeStringLines(String lineOne, String lineTwo, int keyIndex, int insertingIndex)