Java String Split by Char split(String str, char ch, int size)

Here you can find the source of split(String str, char ch, int size)

Description

Splits the specified string around matches of the given delimiting char.

License

Apache License

Parameter

Parameter Description
str the string
ch the delimiting char

Return

the list of strings computed by splitting the string around matches of the given char

Declaration

public static String[] split(String str, char ch, int size) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**/*from   w ww. j a va  2s . com*/
     * Splits the specified string around matches of the given
     * delimiting char.
     *
     * @param str the   string
     * @param ch  the delimiting char
     * @return the list of strings computed by splitting the string around
     * matches of the given char
     */
    public static String[] split(String str, char ch, int size) {
        String[] a = new String[size];
        //long b = System.nanoTime();
        int j = str.indexOf(ch), k = 0, i = 0;
        //logger.info("j = " + j);
        while (j != -1) {
            a[i++] = str.substring(k, j);

            //logger.info(k + "\t" + j + "\t'" + str.substring(k, j) + "'");
            str = str.substring(j + 1, str.length());
            //logger.info(k + "\t" + j + "\t'" + str + "'");
            j = str.indexOf(ch);
            ///logger.info("j = " + j);
        }

        a[i++] = str.substring(j + 1, str.length());
        //logger.info(k + "\t" + j + "\t'" + str + "'");

        return a;
    }

    /**
     * Splits the specified string around matches of the given
     * delimiting char.
     *
     * @param str the   string
     * @param ch  the delimiting char
     * @return the list of strings computed by splitting the string around
     * matches of the given char
     */
    public static List<String> split(String str, char ch) {
        List<String> list = new ArrayList<String>();
        //long b = System.nanoTime();
        int j = str.indexOf(ch), k = 0;
        //logger.info("j = " + j);
        while (j != -1) {
            list.add(str.substring(k, j));

            //logger.info(k + "\t" + j + "\t'" + str.substring(k, j) + "'");
            str = str.substring(j + 1, str.length());
            //logger.info(k + "\t" + j + "\t'" + str + "'");
            j = str.indexOf(ch);
            ///logger.info("j = " + j);
        }

        str = str.substring(j + 1, str.length());
        list.add(str);
        //logger.info(k + "\t" + j + "\t'" + str + "'");

        return list;
    }
}

Related

  1. split(String splittee, String splitChar)
  2. split(String str, char c)
  3. split(String str, char c)
  4. split(String str, char ch)
  5. split(String str, char ch)
  6. split(String str, char cha)
  7. split(String str, char deli)
  8. split(String str, char sep)
  9. split(String str, char splitChar)