Java String Split by Case splitWords(String text, boolean toLowerCase)

Here you can find the source of splitWords(String text, boolean toLowerCase)

Description

split Words

License

Apache License

Declaration

public static List<String> splitWords(String text, boolean toLowerCase) 

Method Source Code


//package com.java2s;
/*/*from   w w  w. j ava 2s  . c o m*/
 * Copyright 2016 Dmitriy Malakhov.
 *
 * 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.ArrayList;
import java.util.List;

public class Main {
    public static List<String> splitWords(String text, boolean toLowerCase) {
        List<String> words = new ArrayList<String>();
        StringBuilder word = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            if (Character.isAlphabetic(text.charAt(i))) {
                word.append(toLowerCase ? Character.toLowerCase(text.charAt(i)) : text.charAt(i));
                continue;
            }
            if (!word.toString().isEmpty()) {
                words.add(word.toString());
                word = new StringBuilder();
            }
        }
        if (!word.toString().isEmpty()) {
            words.add(word.toString());
            word = new StringBuilder();
        }
        return words;
    }
}

Related

  1. splitCamelCase(final String s)
  2. splitCamelCase(String src)
  3. splitCamelCase(String string)
  4. splitOnUppercase(String input)
  5. splitString(String string, Character character, boolean encased)