Java String to List convertTextToStringList(String tagText)

Here you can find the source of convertTextToStringList(String tagText)

Description

Convert tag text to list of seperated words

License

Apache License

Declaration

public static List<String> convertTextToStringList(String tagText) 

Method Source Code

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

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

public class Main {
    /**//  www  . j a v a2 s. com
     * Convert tag text to list of seperated words
     * 
     * @return
     */
    public static List<String> convertTextToStringList(String tagText) {

        List<String> result = new ArrayList<String>();
        StringBuilder builder = new StringBuilder();

        // If characters are all upper case, we wont be able to decipher it
        if (allUpperCase(tagText) || allLowerCase(tagText)) {
            result.add(tagText);
        } else {

            for (int i = tagText.length() - 1; i >= 0; --i) {
                char ch = tagText.charAt(i);

                if (Character.isLowerCase(ch)) {
                    // Most common case
                    builder.insert(0, ch);
                } else if (Character.isUpperCase(ch)) {
                    // Words are separated by Upper Case Character by default
                    // If an upper case character is encountered, separate it
                    builder.insert(0, ch);
                    result.add(0, builder.toString());
                    builder = new StringBuilder();
                } else if (Character.isDigit(ch)) {
                    // Keep numbers together
                    if (builder.length() > 0 && !Character.isDigit(builder.charAt(0))) {
                        // Character before isn't a digit. Break it up
                        // Keep digits together in case they are years or
                        // something
                        result.add(builder.toString());
                        builder = new StringBuilder();
                    }

                    builder.insert(0, ch);
                } else if (ch == '.' || ch == ',' || ch == '!' || ch == '?') {
                    // Word break;
                    result.add(0, builder.toString());
                    // add string
                    result.add(String.valueOf(ch));
                    // reset builder
                    builder = new StringBuilder();

                } else if (!isSpacer(ch)) {
                    if (ch == '\'') {
                        // This is an apostrophe, should be left where it is
                        result.add(0, builder.toString());
                    } else if (builder.length() > 0 && Character.isLetterOrDigit(builder.charAt(0))) {
                        // Add to characters strung together
                        // Could be a Smiley face, heart, or something of the
                        // like
                        result.add(0, builder.toString());
                        builder = new StringBuilder();
                    }
                    // Build string of characters
                    builder.append(ch);
                } else if (builder.length() > 0) {
                    /*
                     * Encountered spacer and builder had text in queue Add
                     * queued text to result list
                     */
                    result.add(0, builder.toString());
                    builder = new StringBuilder();
                } // else, no need to do anything

            }

            if (builder.length() > 0) {
                result.add(0, builder.toString());
            }
        }

        return result;
    }

    /**
     * Determine if all characters in a string are upper case letters
     * 
     * @param tagText
     * @return true if all
     */
    private static boolean allUpperCase(String tagText) {

        for (int i = 0; i < tagText.length(); i++) {
            char c = tagText.charAt(i);
            if (!Character.isUpperCase(c)) {
                return false;
            }
        }

        return true;
    }

    /**
     * Determine if all characters in a string are lower case letters
     * 
     * @param tagText
     * @return
     */
    private static boolean allLowerCase(String tagText) {
        for (int i = 0; i < tagText.length(); i++) {
            char c = tagText.charAt(i);
            if (!Character.isLowerCase(c)) {
                return false;
            }
        }

        return true;
    }

    /**
     * @param ch
     *            - a character
     * @return true if ch is a known replacer of a space
     */
    public static boolean isSpacer(char ch) {
        return (ch == '_' || Character.isWhitespace(ch) || ch == '-');
    }
}

Related

  1. convertStringToEnumList(Class enumType, List stringList)
  2. convertStringToList(String str)
  3. convertStringToList(String string, String delimiter, boolean trim)
  4. convertStringToList(String value, String delimiter)
  5. convertTextToStringList(String text)
  6. convertToList(String inputString)
  7. convertToList(String str)
  8. convertToList(String string, String delim)