Java String Split by Separator split(String string, char separator)

Here you can find the source of split(String string, char separator)

Description

split

License

Apache License

Parameter

Parameter Description
string String
separator char

Return

[]

Declaration

public static String[] split(String string, char separator) 

Method Source Code


//package com.java2s;
/*/*  ww w.j a  v a2s  . c o m*/
 * Copyright 2015 Pegasus Solutions
 * @author Rafael Peres dos Santos
 *
 * 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.
 *
 * ================================================================================
 *
 * Direitos autorais 2015 Pegasus Solutions
 * @author Rafael Peres dos Santos
 * 
 * Licenciado sob a Licen?a Apache, Vers?o 2.0 ("LICEN?A"); voc? n?o pode usar
 * esse arquivo exceto em conformidade com a esta LICEN?A. Voc? pode obter uma
 * c?pia desta LICEN?A em http://www.apache.org/licenses/LICENSE-2.0 A menos que
 * haja exig?ncia legal ou acordo por escrito, a distribui??o de software sob
 * esta LICEN?A se dar? ?COMO EST????, SEM GARANTIAS OU CONDI??ES DE QUALQUER
 * TIPO, sejam expressas ou t?citas. Veja a LICEN?A para a reda??o espec?fica a
 * reger permiss?es e limita??es sob esta LICEN?A.
 *
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * split 
     * 
     * @param string {@link String}
     * @param separator char
     * @return {@link String}[]
     */
    public static String[] split(String string, char separator) {
        List<String> tokens = new ArrayList<String>();
        int idxBeforeComma = 0;
        for (int i = 0; i < string.length(); i++) {
            if (string.charAt(i) == separator) {
                tokens.add(string.substring(idxBeforeComma, i));
                idxBeforeComma = i + 1;
            }
        }

        return tokens.toArray(new String[] {});
    }
}

Related

  1. split(String str, char separatorChar)
  2. split(String str, char separatorChar, boolean preserveAllTokens)
  3. split(String str, String separator)
  4. split(String str, String separator)
  5. split(String str, String separator, boolean preserveEmptyToken)
  6. split(String strToSplit, String strSeparator, int iLimit)
  7. split(String text, String separators, String brackets)
  8. splitAll(String str, char separatorChar)
  9. splitAndTrim(String tags, String separator)