Java Scanner Usage getNextWord(String str, int index, StringBuilder outToken, String ignoreRegex)

Here you can find the source of getNextWord(String str, int index, StringBuilder outToken, String ignoreRegex)

Description

Retrieves the next word in str.

License

Open Source License

Parameter

Parameter Description
str the string
index index of start search
outToken holds new token on return if non-null
ignoreRegex characters to skip when looking for next word

Return

index in raw of start of next word

Declaration

public static int getNextWord(String str, int index, StringBuilder outToken, String ignoreRegex) 

Method Source Code

//package com.java2s;
/*/*w w w . j a  v a  2  s  .c o m*/
 * Copyright (c) 2013 Game Salutes.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 * 
 * Contributors:
 *     Game Salutes - Repackaging and modifications of original work under University of Chicago and Apache License 2.0 shown below
 * 
 * Repackaging from edu.uchicago.nsit.iteco.utils to com.gamesalutes.utils
 * 
 * Copyright 2008 - 2011 University of Chicago
 * 
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * 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.Scanner;

public class Main {
    /**
     * Retrieves the next word in <code>str</code>.
     * 
     * @param str the string
     * @param index index of start search
     * @param outToken holds new token on return if non-null
     * @param ignoreRegex characters to skip when looking for next word 
     * @return index in raw of start of next word
     */
    public static int getNextWord(String str, int index, StringBuilder outToken, String ignoreRegex) {
        if (index >= str.length())
            return str.length();

        String rem = str.substring(index, str.length());
        Scanner s = new Scanner(rem);
        s.useDelimiter(ignoreRegex);
        String token = null;

        while (s.hasNext() && (token = s.next()).length() == 0)
            ;

        if (token != null && token.length() != 0) {
            if (outToken != null)
                outToken.append(token);
            return str.indexOf(token, index);
        } else
            return str.length();
        //end of string
        //if(!s.hasNext())
        //   return str.length();
        //else
        //return str.indexOf(token,index);
    }
}

Related

  1. getLngIn()
  2. getNextInput(Scanner scan)
  3. getNextLine(final Scanner scanner)
  4. getNextLine(final Scanner source)
  5. getNextWord(Scanner scan)
  6. getNumberOfUsernames(String usernames)
  7. getOutputFromValgrindOutput(String valgrindOutput)
  8. getPartOfString(String string, int index, String delimiter)
  9. getReplay(String prompt)