Java String Chop at Word chopAtWordsAround(String input, String wordList[], int numChars)

Here you can find the source of chopAtWordsAround(String input, String wordList[], int numChars)

Description

chop At Words Around

License

Open Source License

Declaration

public static String chopAtWordsAround(String input, String wordList[], int numChars) 

Method Source Code

//package com.java2s;
/**/* w w w  .  j ava  2s  .c  o m*/
 * $Revision $
 * $Date $
 *
 * Copyright (C) 2005-2010 Jive Software. All rights reserved.
 *
 * 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.
 */

public class Main {
    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();
        String arr$[] = wordList;
        int len$ = arr$.length;
        for (int i$ = 0; i$ < len$; i$++) {
            String aWordList = arr$[i$];
            int pos = lc.indexOf(aWordList);
            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[];
                for (chars = input.toCharArray(); beginIdx > 0 && chars[beginIdx] != ' ' && chars[beginIdx] != '\n'
                        && chars[beginIdx] != '\r'; beginIdx--)
                    ;
                for (; endIdx < input.length() && chars[endIdx] != ' ' && chars[endIdx] != '\n'
                        && chars[endIdx] != '\r'; endIdx++)
                    ;
                return input.substring(beginIdx, endIdx);
            }
        }

        return input.substring(0, input.length() < 200 ? input.length() : 200);
    }
}

Related

  1. chopAtWord(String string, int length)
  2. chopAtWord(String string, int length)
  3. chopAtWord(String string, int length, int minLength)
  4. chopAtWord(String string, int length, int minLength)
  5. chopAtWordsAround(String input, String[] wordList, int numChars)