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

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

Description

Description:split a string into String array delimited by character

License

Open Source License

Parameter

Parameter Description
str the original string
deli the delimit character

Return

the String array or a zero length string array

Declaration

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

Method Source Code


//package com.java2s;
/*/*  w ww.j  a v  a 2  s.c  o m*/
 * $RCSfile: LogStringUtil,v $$
 * $Revision: 1.0  $
 * $Date: 2010-12-09  $
 *
 * Copyright (C) 2011 GyTech, Inc. All rights reserved.
 *
 * This software is the proprietary information of GyTech, Inc.
 * Use is subject to license terms.
 */

import java.util.ArrayList;

public class Main {
    /**
     * <p>Description:split a string into String array delimited by character</p>
     * @param str the original string
     * @param deli the delimit character
     * @return the String array or a zero length string array
     */
    public static String[] split(String str, char deli) {
        // return no groups if we have an empty string
        if ((str == null) || "".equals(str)) {
            return new String[0];
        }

        ArrayList<String> parts = new ArrayList<String>();
        int currIdx;
        int prevIdx = 0;

        while ((currIdx = str.indexOf(deli, prevIdx)) > 0) {
            String part = str.substring(prevIdx, currIdx).trim();
            parts.add(part);
            prevIdx = currIdx + 1;
        }

        parts.add(str.substring(prevIdx, str.length()).trim());

        String[] result = new String[parts.size()];
        parts.toArray(result);

        return result;
    }

    /**
     * <p>Description:Trim the input string</p>
     * @param input string
     * @return the trimmed string
     */
    public static String trim(String str) {
        return (str == null ? "" : str.trim());
    }

    /**
    * <p>Description:Sub a string with the length, </p>
    * @param srcStr source string
    * @param subLen the length what you want to sub a string
    * @return subLen the length of sub string
    */
    public static String subString(String srcStr, int subLen) {
        byte[] bytes = srcStr.getBytes();
        //add by yujie
        //when logwarn system will remove some character of chinese string
        //on 2005/10/18
        if (bytes.length <= subLen)
            return srcStr;

        char[] chars = srcStr.toCharArray();
        StringBuffer sb = new StringBuffer();

        for (int i = 0, j = 0; i < subLen && i < bytes.length && j < chars.length;) {
            char cbyte = (char) bytes[i];
            //normal English character
            if (cbyte == chars[j]) {
                sb.append(chars[j]);
                i++;
                j++;
            } else {//Chinese character
                    //if adding next Chinese character will be over limitted, 
                    //then will not add this character
                if (i + 1 < subLen) {
                    sb.append(chars[j]);
                    //one chinese character will be equal two bytes
                    i++;
                    i++;
                    j++;
                } else
                    break;
            }
        }

        return sb.toString();
    }
}

Related

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