Java String Split by Char split(String s, char c)

Here you can find the source of split(String s, char c)

Description

Splits a character-delimited string into a string array.

License

Apache License

Parameter

Parameter Description
s The string to split. Can be <jk>null</jk>.
c The character to split on.

Return

The tokens.

Declaration

public static String[] split(String s, char c) 

Method Source Code

//package com.java2s;
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *

import java.util.*;

public class Main {
    /**//  w  ww  .j  av  a 2 s.  com
     * Splits a character-delimited string into a string array.
     * Does not split on escaped-delimiters (e.g. "\,");
     * Resulting tokens are trimmed of whitespace.
     * <b>NOTE:</b>  This behavior is different than the Jakarta equivalent.
     * split("a,b,c",',') -> {"a","b","c"}
     * split("a, b ,c ",',') -> {"a","b","c"}
     * split("a,,c",',') -> {"a","","c"}
     * split(",,",',') -> {"","",""}
     * split("",',') -> {}
     * split(null,',') -> null
     * split("a,b\,c,d", ',', false) -> {"a","b\,c","d"}
     * split("a,b\\,c,d", ',', false) -> {"a","b\","c","d"}
     * split("a,b\,c,d", ',', true) -> {"a","b,c","d"}
     *
     * @param s The string to split.  Can be <jk>null</jk>.
     * @param c The character to split on.
     * @return The tokens.
     */
    public static String[] split(String s, char c) {

        char[] unEscapeChars = new char[] { '\\', c };

        if (s == null)
            return null;
        if (isEmpty(s))
            return new String[0];
        if (s.indexOf(c) == -1)
            return new String[] { s };

        List<String> l = new LinkedList<String>();
        char[] sArray = s.toCharArray();
        int x1 = 0, escapeCount = 0;
        for (int i = 0; i < sArray.length; i++) {
            if (sArray[i] == '\\')
                escapeCount++;
            else if (sArray[i] == c && escapeCount % 2 == 0) {
                String s2 = new String(sArray, x1, i - x1);
                String s3 = unEscapeChars(s2, unEscapeChars);
                l.add(s3.trim());
                x1 = i + 1;
            }
            if (sArray[i] != '\\')
                escapeCount = 0;
        }
        String s2 = new String(sArray, x1, sArray.length - x1);
        String s3 = unEscapeChars(s2, unEscapeChars);
        l.add(s3.trim());

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

    /**
     * Same as {@link #split(String, char)} except splits all strings in the input and returns a single result.
     *
     * @param s The string to split.  Can be <jk>null</jk>.
     * @param c The character to split on.
     * @return The tokens.
     */
    public static String[] split(String[] s, char c) {
        if (s == null)
            return null;
        List<String> l = new LinkedList<String>();
        for (String ss : s) {
            if (ss == null || ss.indexOf(c) == -1)
                l.add(ss);
            else
                l.addAll(Arrays.asList(split(ss, c)));
        }
        return l.toArray(new String[l.size()]);
    }

    /**
     * Returns <jk>true</jk> if specified string is <jk>null</jk> or empty.
     *
     * @param s The string to check.
     * @return <jk>true</jk> if specified string is <jk>null</jk> or empty.
     */
    public static boolean isEmpty(String s) {
        return s == null || s.isEmpty();
    }

    /**
     * Returns <jk>true</jk> if specified string is <jk>null</jk> or it's {@link #toString()} method returns an empty string.
     *
     * @param s The string to check.
     * @return <jk>true</jk> if specified string is <jk>null</jk> or it's {@link #toString()} method returns an empty string.
     */
    public static boolean isEmpty(Object s) {
        return s == null || s.toString().isEmpty();
    }

    /**
     * Removes escape characters (\) from the specified characters.
     *
     * @param s The string to remove escape characters from.
     * @param toEscape The characters escaped.
     * @return A new string if characters were removed, or the same string if not or if the input was <jk>null</jk>.
     */
    public static String unEscapeChars(String s, char[] toEscape) {
        return unEscapeChars(s, toEscape, '\\');
    }

    /**
     * Removes escape characters (specified by escapeChar) from the specified characters.
     *
     * @param s The string to remove escape characters from.
     * @param toEscape The characters escaped.
     * @param escapeChar The escape character.
     * @return A new string if characters were removed, or the same string if not or if the input was <jk>null</jk>.
     */
    public static String unEscapeChars(String s, char[] toEscape, char escapeChar) {
        if (s == null)
            return null;
        if (s.length() == 0 || toEscape == null || toEscape.length == 0 || escapeChar == 0)
            return s;
        StringBuffer sb = new StringBuffer(s.length());
        char[] sArray = s.toCharArray();
        for (int i = 0; i < sArray.length; i++) {
            char c = sArray[i];

            if (c == escapeChar) {
                if (i + 1 != sArray.length) {
                    char c2 = sArray[i + 1];
                    boolean isOneOf = false;
                    for (int j = 0; j < toEscape.length && !isOneOf; j++)
                        isOneOf = (c2 == toEscape[j]);
                    if (isOneOf) {
                        i++;
                    } else if (c2 == escapeChar) {
                        sb.append(escapeChar);
                        i++;
                    }
                }
            }
            sb.append(sArray[i]);
        }
        return sb.toString();
    }

    /**
     * Same as {@link String#trim()} but prevents <code>NullPointerExceptions</code>.
     *
     * @param s The string to trim.
     * @return The trimmed string, or <jk>null</jk> if the string was <jk>null</jk>.
     */
    public static String trim(String s) {
        if (s == null)
            return null;
        return s.trim();
    }

    /**
     * Calls {@link #toString()} on the specified object if it's not null.
     *
     * @param o The object to convert to a string.
     * @return The object converted to a string, or <jk>null</jk> if the object was null.
     */
    public static String toString(Object o) {
        return (o == null ? null : o.toString());
    }
}

Related

  1. split(char sep, String input)
  2. split(final String input, final char split)
  3. split(final String string, final char... toSplit)
  4. split(String cs, char sep)
  5. split(String s, char c)
  6. split(String s, char c)
  7. split(String s, char c, int limit)
  8. split(String s, char c, int limit)
  9. split(String s, char ch)