Java String Split by Separator split(String str, char separator)

Here you can find the source of split(String str, char separator)

Description

split

License

MIT License

Declaration

public static String[] split(String str, char separator) 

Method Source Code

//package com.java2s;
/*!//from  w ww.  j  av  a 2s .  co m
 * mifmi-commons4j
 * https://github.com/mifmi/mifmi-commons4j
 *
 * Copyright (c) 2015 mifmi.org and other contributors
 * Released under the MIT license
 * https://opensource.org/licenses/MIT
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static String[] split(String str, char separator) {
        return split(str, separator, false, -1, -1, false);
    }

    public static String[] split(String str, char separator, boolean trim, int maxCount, int escapeChar,
            boolean unescape) {
        if (str == null) {
            return null;
        }

        if (str.isEmpty() || maxCount == 0) {
            return new String[0];
        }

        int sepCnt = count(str, separator);
        if (sepCnt == 0) {
            return new String[] { str };
        }

        int strLen = str.length();
        StringBuilder sb = new StringBuilder(strLen);
        boolean escaped = false;
        List<String> list = new ArrayList<>(sepCnt + 1);
        for (int i = 0; i < strLen; i++) {
            char ch = str.charAt(i);
            if (escaped) {
                sb.append(ch);
                if (!Character.isHighSurrogate(ch)) {
                    escaped = false;
                }
            } else {
                if (0 <= escapeChar && ch == escapeChar) {
                    if (!unescape) {
                        sb.append(ch);
                    }
                    escaped = true;
                } else if (ch == separator) {
                    if (maxCount < 0 || list.size() < maxCount - 1) {
                        list.add(toString(sb, trim));
                        sb.setLength(0);
                    } else {
                        sb.append(ch);
                    }
                } else {
                    sb.append(ch);
                }
            }
        }
        list.add(toString(sb, trim));

        return list.toArray(new String[list.size()]);
    }

    public static String[] split(String str, String separatorRegex) {
        return split(str, separatorRegex, false);
    }

    public static String[] split(String str, String separatorRegex, boolean trim) {
        if (str == null) {
            return null;
        }
        if (separatorRegex == null) {
            throw new NullPointerException();
        }
        if (trim) {
            str = str.trim();
            separatorRegex = "\\s*" + separatorRegex + "\\s*";
        }

        if (str.isEmpty()) {
            return new String[0];
        }

        return str.split(separatorRegex, -1);
    }

    public static int count(CharSequence str, char ch) {
        if (str == null) {
            return 0;
        }

        int count = 0;
        int len = str.length();
        for (int i = 0; i < len; i++) {
            if (str.charAt(i) == ch) {
                count++;
            }
        }

        return count;
    }

    public static String toString(StringBuilder sb, boolean trim) {
        if (sb == null) {
            return null;
        }

        if (!trim) {
            return sb.toString();
        }

        int len = sb.length();
        if (len == 0) {
            return "";
        }

        int sIdx;
        for (sIdx = 0; sIdx < len; sIdx++) {
            char ch = sb.charAt(sIdx);
            if (!Character.isWhitespace(ch)) {
                break;
            }
        }

        if (sIdx == len) {
            return "";
        }

        int eIdx;
        for (eIdx = len - 1; 0 <= eIdx; eIdx--) {
            char ch = sb.charAt(eIdx);
            if (!Character.isWhitespace(ch)) {
                break;
            }
        }

        return sb.substring(sIdx, eIdx + 1);
    }
}

Related

  1. split(String s, String separator)
  2. split(String s, String separator)
  3. split(String s, String separator)
  4. split(String src, char separator)
  5. split(String src, char separator, boolean trim)
  6. split(String str, char separator)
  7. split(String str, char separator)
  8. split(String str, char separator)
  9. split(String str, char separatorChar)