Java String Split by Char split(String splittee, String splitChar)

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

Description

split

License

Open Source License

Declaration

public static String[] split(String splittee, String splitChar) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    public static String[] split(String splittee, String splitChar) {
        return split(splittee, splitChar, 0);
    }// w w w.  ja  v  a 2s. c  o m

    /**
     * String split method
     * @param splittee  input string
     * @param splitChar split text
     * @param limit split limit number
     * @return  String[]
     */
    public static String[] split(String splittee, String splitChar, int limit) {
        String taRetVal[];
        StringTokenizer toTokenizer;
        int tnTokenCnt;

        try {
            toTokenizer = new StringTokenizer(splittee, splitChar);
            tnTokenCnt = toTokenizer.countTokens();
            if (limit != 0 && tnTokenCnt > limit)
                tnTokenCnt = limit;
            taRetVal = new String[tnTokenCnt];

            for (int i = 0; i < tnTokenCnt; i++) {
                if (toTokenizer.hasMoreTokens()) {
                    taRetVal[i] = toTokenizer.nextToken();
                }
                if (limit != 0 && limit == (i + 1))
                    break;
            }
        } catch (Exception e) {
            taRetVal = new String[0];
        }
        return taRetVal;
    }
}

Related

  1. split(String s, char ch)
  2. split(String s, char divider)
  3. split(String s, char divider, String[] output)
  4. split(String s, char sep)
  5. split(String s, char sep)
  6. split(String str, char c)
  7. split(String str, char c)
  8. split(String str, char ch)
  9. split(String str, char ch)