Java String Split by Char split(String str, char splitChar)

Here you can find the source of split(String str, char splitChar)

Description

Parse a string into a series of string tokens using the specified delimiter.

License

Open Source License

Parameter

Parameter Description
str a parameter
splitChar a parameter

Return

Array of string token

Declaration

public static String[] split(String str, char splitChar) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;

public class Main {
    /**//from w w  w .  ja  v  a 2s.  co m
     * Parse a string into a series of string tokens using the specified
     * delimiter.
     * 
     * @param str
     * @param splitChar
     * @return Array of string token
     */
    public static String[] split(String str, char splitChar) {
        if (str == null) {
            return null;
        }
        if (str.trim().equals("")) {
            return new String[0];
        }
        if (str.indexOf(splitChar) == -1) {
            String[] strArray = new String[1];
            strArray[0] = str;
            return strArray;
        }

        ArrayList<String> list = new ArrayList<String>();
        int prevPos = 0;
        for (int pos = str.indexOf(splitChar); pos >= 0; pos = str.indexOf(splitChar, (prevPos = (pos + 1)))) {
            list.add(str.substring(prevPos, pos));
        }
        list.add(str.substring(prevPos, str.length()));

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

Related

  1. split(String str, char ch)
  2. split(String str, char ch, int size)
  3. split(String str, char cha)
  4. split(String str, char deli)
  5. split(String str, char sep)
  6. split(String str, char splitChar)
  7. Split(String Str, char splitchar)
  8. split(String string, char c)
  9. split(String string, char character)