Java String Split by Delimiter split(String stringToSplit, char delimiter)

Here you can find the source of split(String stringToSplit, char delimiter)

Description

Split a string into tokens identified by a delimiter character.

License

Apache License

Parameter

Parameter Description
stringToSplit the string to split
delimiter the delimiter character to find in the string

Return

the string separated into tokens identified by the delimiter, null if stringToSplit is null

Declaration

public final static List<String> split(String stringToSplit, char delimiter) 

Method Source Code


//package com.java2s;
/*/* ww w .j av a2 s .  c  o  m*/
 * Copyright (c) 2013 Paul Mackinlay, Ramon Servadei 
 *  
 * 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 {
    /**
     * Split a string into tokens identified by a delimiter character. The delimiter character is
     * 'escaped' by a preceeding occurrence of the delimiter character, e.g:<br>
     * 
     * <pre>
     * If the delimiter is ','
     * "token1,token2" -> "token1" "token2"
     * "token1,,,token2" -> "token1," "token2"
     * </pre>
     * 
     * @param stringToSplit
     *            the string to split
     * @param delimiter
     *            the delimiter character to find in the string
     * @return the string separated into tokens identified by the delimiter, <code>null</code> if
     *         stringToSplit is <code>null</code>
     */
    public final static List<String> split(String stringToSplit, char delimiter) {
        if (stringToSplit == null) {
            return null;
        }

        final char[] chars = stringToSplit.toCharArray();
        List<String> tokens = new ArrayList<String>();
        StringBuilder sb = new StringBuilder();
        char c;
        boolean lastWasDelimiter = false;
        for (int i = 0; i < chars.length; i++) {
            c = chars[i];
            if (c == delimiter) {
                if (lastWasDelimiter) {
                    // add the token to the buffer
                    sb.append(c);
                    lastWasDelimiter = false;
                } else {
                    lastWasDelimiter = true;
                }
            } else {
                if (lastWasDelimiter) {
                    // a single token is a separator
                    tokens.add(sb.toString());
                    sb.setLength(0);
                    lastWasDelimiter = false;
                }
                sb.append(c);
            }
        }
        tokens.add(sb.toString());
        return tokens;
    }
}

Related

  1. split(String str, String delims)
  2. split(String str, String regexDelim, int limitUseRegex)
  3. split(String string, String delim, boolean doTrim)
  4. split(String string, String delimiter)
  5. split(String string, String delimiter)
  6. split(String strToSplit, char delimiter)
  7. split(String text, String delimiter)
  8. split(String value, char delim)
  9. split(String value, char delimiter)