Java String Split split(String text)

Here you can find the source of split(String text)

Description

Split text into chinese\number\letter\punct.

License

Apache License

Parameter

Parameter Description
text The text to be split.

Return

The array of the result.

Declaration

public static String[] split(String text) 

Method Source Code

//package com.java2s;
/*//from   www  .  j  a  va  2s .c  om
 * Copyright 2016 liufanping@iveely.com.
 *
 * 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 {
    /**
     * Split text into chinese\number\letter\punct.
     *
     * @param text The text to be split.
     * @return The array of the result.
     */
    public static String[] split(String text) {
        // 1. Check null.
        if (text == null) {
            return new String[0];
        }

        // 2.Split.
        List<String> list = new ArrayList<>();
        int flag = -1; // chinese(2)\number(0)\letter(1)
        String temp = "";
        char[] array = text.toCharArray();
        for (char c : array) {
            if (isNumeric(c)) {
                if (flag != 0 && temp.length() > 0) {
                    list.add(temp);
                    temp = "";
                }
                flag = 0;
                temp += c;
            } else if (isLetter(c) || c == 60 || c == 62 || c == 47 || c == 91 || c == 93) {
                if (flag != 1 && temp.length() > 0) {
                    list.add(temp);
                    temp = "";
                    temp += c;
                    flag = 1;
                } else if (flag == 1 && temp.length() > 0 && (c == 62 || c == 93)) {
                    list.add(temp + c);
                    temp = "";
                } else {
                    flag = 1;
                    temp += c;
                }

            } else {
                if (temp.length() > 0) {
                    list.add(temp);
                    temp = "";
                }
                list.add(c + "");
                flag = 2;
            }
        }
        if (flag < 2) {
            list.add(temp);
        }
        String[] ret = new String[list.size()];
        return list.toArray(ret);
    }

    /**
     * Check the char whether is a number.
     *
     * @param c The char to be check.
     * @return true is number, or is not.
     */
    public static boolean isNumeric(char c) {
        return Character.isDigit(c);
    }

    /**
     * Check the char whether is a letter.
     *
     * @param c The char to be check.
     * @return true is a letter, or is not.
     */
    public static boolean isLetter(char c) {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }
}

Related

  1. split(String str, String token)
  2. split(String string)
  3. split(String string)
  4. split(String string, String seperator)
  5. split(String strSource, String strReg)
  6. split(String text)
  7. split(String text)
  8. split(String text, String token)
  9. split(String token, String s)