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

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

Description

Split a string using indexOf and subsString, this is proved to be faster than String.split and StringUtils.split

License

Apache License

Parameter

Parameter Description
strToSplit a parameter
delimiter a parameter

Declaration

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

Method Source Code

//package com.java2s;
/**//w  w  w  .  j av  a  2 s  .  c o  m
 * The contents of this file may be used under the terms of the Apache License, Version 2.0
 * in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above.
 *
 * Copyright 2014, Ecarf.io
 *
 * 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 using indexOf and subsString, this is proved to be 
     * faster than String.split and StringUtils.split
     * @param strToSplit
     * @param delimiter
     * @return
     */
    public static List<String> split(String strToSplit, char delimiter) {

        List<String> parts = new ArrayList<>();
        int foundPosition;
        int startIndex = 0;
        String part;
        while ((foundPosition = strToSplit.indexOf(delimiter, startIndex)) > -1) {
            part = strToSplit.substring(startIndex, foundPosition);
            if (part.length() > 0) {
                parts.add(part);
            }
            startIndex = foundPosition + 1;
        }
        if (startIndex < strToSplit.length()) {
            parts.add(strToSplit.substring(startIndex));
        }
        return parts;

    }
}

Related

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