Java String Split by Word splitWords(String name)

Here you can find the source of splitWords(String name)

Description

split Words

License

Apache License

Declaration

public static List<String> splitWords(String name) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<String> splitWords(String name) {
        name = name.replaceAll("_{2,}", "_");
        name = name.replaceAll("-{2,}", "-");

        List<String> result = new ArrayList<>();
        StringBuilder currentWord = new StringBuilder(name.length());

        for (int i = 0, len = name.length(); i < len; i++) {
            char c = name.charAt(i);
            char next = i == len - 1 ? '\0' : name.charAt(i + 1);
            char prev = i == 0 ? '\0' : name.charAt(i - 1);

            if ((c == '_' || c == '-') && !(Character.isDigit(next) && Character.isDigit(prev))) {
                if (currentWord.length() > 0) {
                    result.add(currentWord.toString());
                    currentWord.setLength(0);
                }/*from  w w  w.ja v a 2s. co  m*/
                continue;
            }

            if (Character.isUpperCase(c) && (!Character.isUpperCase(prev) || Character.isLowerCase(next))) {
                if (currentWord.length() > 0) {
                    result.add(currentWord.toString());
                    currentWord.setLength(0);
                }
            }

            currentWord.append(c);
        }
        result.add(currentWord.toString());
        return result;
    }
}

Related

  1. splitIntoWords(String s)
  2. splitKeyWords(String sql)
  3. splitOne(String wordString)
  4. splitUnit(String word)
  5. splitWord(List words, int listIndex)
  6. splitwords(String s)
  7. splitwords(String text)
  8. splitWords(String text, String splitter)