Java String Split by Char splitTrim(String str, String limitChar)

Here you can find the source of splitTrim(String str, String limitChar)

Description

split Trim

License

Apache License

Declaration

public static String[] splitTrim(String str, String limitChar) 

Method Source Code

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

import java.util.ArrayList;

import java.util.List;

public class Main {

    public static String[] splitTrim(String str, String limitChar) {
        if (str == null) {
            return null;
        }//  www  .j  a  va2  s  .c  om
        if ("".equals(str) || limitChar == null || "".equals(limitChar)) {
            return new String[] { str };
        }
        int limitLength = limitChar.length();
        if (limitLength > str.length()) {
            return new String[] { str };
        }
        List<String> list = new ArrayList<String>();
        int start = 0;
        for (int i = 0; i < str.length(); i++) {
            if (i + limitLength > str.length()) {
                break;
            }
            String cur = str.substring(i, i + limitLength);
            if (limitChar.equals(cur)) {
                list.add(str.substring(start, i));
                start = i + limitLength;
                i += limitLength - 1;
            }
        }
        list.add(str.substring(start));
        int splitLength = list.size();
        String[] result = new String[splitLength];
        for (int i = 0; i < splitLength; i++) {
            result[i] = list.get(i).trim();
        }
        return result;
    }

    public static String[] splitTrim(String str) {
        return splitTrim(str, ",");
    }
}

Related

  1. SplitSearchString(String search_string, char split_char, boolean clear_exact_match_char)
  2. splitString(final String s, final char c, final boolean trimBlanks)
  3. splitString(String str, char sep)
  4. splitString(String string, char ch)
  5. splitStringToChars(String str)
  6. splitWithEscape(String s, String splitChars)