Returns a substring of the given string which represents the words around the given word. - Java java.lang

Java examples for java.lang:String Substring

Description

Returns a substring of the given string which represents the words around the given word.

Demo Code

/**//from   w w w .  j ava  2s  .c  o  m
 * $RCSFile$
 * $Revision: 18401 $
 * $Date: 2005-02-05 10:18:52 -0800 (Sat, 05 Feb 2005) $
 *
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution, or a commercial license
 * agreement with Jive.
 */
//package com.java2s;

public class Main {
    /**
     * Returns a substring of the given string which represents the words around the given word.
     * For example, passing in "This is a quick test a test", "{a,test}" and 5 would return a string
     * of "This is a quick" - that's 5 characters (or to the end of the word, whichever
     * is greater) on either side of "a". Also, since {a,test} is passed in a "a" is found
     * first in the string, we base the substring off of the position of "a". The wordList is
     * really just a list of strings to try - the first one found is used.<p>
     * <p/>
     * Note: The wordList passed in should be lowercase.
     *
     * @param input    The string to parse.
     * @param wordList The words to look for - the first one found in the string is used.
     * @param numChars The number of characters on either side to include in the chop.
     * @return a substring of the given string matching the criteria, otherwise "".
     */
    public static String chopAtWordsAround(String input, String[] wordList,
            int numChars) {
        if (input == null || "".equals(input.trim()) || wordList == null
                || wordList.length == 0 || numChars == 0) {
            return "";
        }
        String lc = input.toLowerCase();
        for (int i = 0; i < wordList.length; i++) {
            int pos = lc.indexOf(wordList[i]);
            if (pos > -1) {
                int beginIdx = pos - numChars;
                if (beginIdx < 0) {
                    beginIdx = 0;
                }
                int endIdx = pos + numChars;
                if (endIdx > input.length() - 1) {
                    endIdx = input.length() - 1;
                }
                char[] chars = input.toCharArray();
                while (beginIdx > 0 && chars[beginIdx] != ' '
                        && chars[beginIdx] != '\n'
                        && chars[beginIdx] != '\r') {
                    beginIdx--;
                }
                while (endIdx < input.length() && chars[endIdx] != ' '
                        && chars[endIdx] != '\n' && chars[endIdx] != '\r') {
                    endIdx++;
                }
                return input.substring(beginIdx, endIdx);
            }
        }
        return input.substring(0,
                (input.length() >= 200) ? 200 : input.length());
    }
}

Related Tutorials