Java String Split by Char splitOnChar(String str, int ch)

Here you can find the source of splitOnChar(String str, int ch)

Description

A faster way to split on a single char than String#split(), since we'll be doing this in a tight loop possibly thousands of times (rt.jar).

License

BSD License

Parameter

Parameter Description
str The string to split.
ch The char to split on.

Return

The string, split on the character (e.g. '/' or '.').

Declaration

public static final String[] splitOnChar(String str, int ch) 

Method Source Code

//package com.java2s;
/*/*from ww w .j  av  a2s. co  m*/
 * 03/21/2010
 *
 * Copyright (C) 2010 Robert Futrell
 * robert_futrell at users.sourceforge.net
 * http://fifesoft.com/rsyntaxtextarea
 *
 * This library is distributed under a modified BSD license.  See the included
 * RSTALanguageSupport.License.txt file for details.
 */

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

public class Main {
    /**
     * A faster way to split on a single char than String#split(), since
     * we'll be doing this in a tight loop possibly thousands of times (rt.jar).
     * This is also fundamentally different than {@link String#split(String)}),
     * in the case where <code>str</code> ends with <code>ch</code> - this
     * method will return an empty item at the end of the returned array, while
     * String#split() will not.
     *
     * @param str The string to split.
     * @param ch The char to split on.
     * @return The string, split on the character (e.g. '<tt>/</tt>' or
     *         '<tt>.</tt>').
     */
    public static final String[] splitOnChar(String str, int ch) {
        List<String> list = new ArrayList<String>(3);
        int pos = 0;
        int old = 0;
        while ((pos = str.indexOf(ch, old)) > -1) {
            list.add(str.substring(old, pos));
            old = pos + 1;
        }
        // If str ends in ch, this adds an empty item to the end of the list.
        // This is what we want.
        list.add(str.substring(old));
        String[] array = new String[list.size()];
        return list.toArray(array);
    }

    /**
     * Returns the next location of a single character in a character sequence.
     * This method is here because <tt>StringBuilder</tt> doesn't get this
     * method added to it until Java 1.5.
     *
     * @param ch The character to look for.
     * @param sb The character sequence.
     * @param offs The offset at which to start looking.
     * @return The next location of the character, or <tt>-1</tt> if it is not
     *         found.
     */
    private static final int indexOf(char ch, CharSequence sb, int offs) {
        while (offs < sb.length()) {
            if (ch == sb.charAt(offs)) {
                return offs;
            }
            offs++;
        }
        return -1;
    }
}

Related

  1. splitEncolosed(String s, char open_tag, char close_tag)
  2. splitFast3(String data, char splitChar)
  3. splitInclusive(CharSequence input, char[] splitChars)
  4. splitList(String source, char useChar)
  5. splitOnChar(String str, char c)
  6. splitOnCharArray(String value, String splitOn)
  7. SplitSearchString(String search_string, char split_char, boolean clear_exact_match_char)
  8. splitString(final String s, final char c, final boolean trimBlanks)
  9. splitString(String str, char sep)