Java String Split by Char splitString(final String s, final char c, final boolean trimBlanks)

Here you can find the source of splitString(final String s, final char c, final boolean trimBlanks)

Description

Optimized version of String#split that doesn't use regexps.

License

Apache License

Parameter

Parameter Description
s The string to split.
c The separator to use to split the string.
trimBlanks true to not return any whitespace only array items

Return

A non-null, non-empty array. <p>Copied from <a href="http://opentsdb.net">OpenTSDB</a>.

Declaration

public static String[] splitString(final String s, final char c, final boolean trimBlanks) 

Method Source Code


//package com.java2s;
/*/* ww w.ja  v a2s.c  o  m*/
 * Copyright 2015 the original author or authors.
 *
 * 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 {
    /**
     * Optimized version of {@code String#split} that doesn't use regexps.
     * This function works in O(5n) where n is the length of the string to
     * split.
     * @param s The string to split.
     * @param c The separator to use to split the string.
     * @param trimBlanks true to not return any whitespace only array items
     * @return A non-null, non-empty array.
     * <p>Copied from <a href="http://opentsdb.net">OpenTSDB</a>.
     */
    public static String[] splitString(final String s, final char c, final boolean trimBlanks) {
        final char[] chars = s.toCharArray();
        int num_substrings = 1;
        final int last = chars.length - 1;
        for (int i = 0; i <= last; i++) {
            char x = chars[i];
            if (x == c) {
                num_substrings++;
            }
        }
        final String[] result = new String[num_substrings];
        final int len = chars.length;
        int start = 0; // starting index in chars of the current substring.
        int pos = 0; // current index in chars.
        int i = 0; // number of the current substring.
        for (; pos < len; pos++) {
            if (chars[pos] == c) {
                result[i++] = new String(chars, start, pos - start);
                start = pos + 1;
            }
        }
        result[i] = new String(chars, start, pos - start);
        if (trimBlanks) {
            int blanks = 0;
            final List<String> strs = new ArrayList<String>(result.length);
            for (int x = 0; x < result.length; x++) {
                if (result[x].trim().isEmpty()) {
                    blanks++;
                } else {
                    strs.add(result[x]);
                }
            }
            if (blanks == 0)
                return result;
            return strs.toArray(new String[result.length - blanks]);
        }
        return result;
    }
}

Related

  1. splitList(String source, char useChar)
  2. splitOnChar(String str, char c)
  3. splitOnChar(String str, int ch)
  4. splitOnCharArray(String value, String splitOn)
  5. SplitSearchString(String search_string, char split_char, boolean clear_exact_match_char)
  6. splitString(String str, char sep)
  7. splitString(String string, char ch)
  8. splitStringToChars(String str)
  9. splitTrim(String str, String limitChar)