split
public String[] split(String regex)
Splits this string around matches of the given regular expression. 
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array. 
***************************
Trailing empty strings are therefore not included in the resulting array. 

解决方案 »

  1.   

    split只能拆分普通的分隔符,要想拆分特殊的分隔符(比如$,//,@),那你就要用StringTokenizer我给你一个例子,你好好看看,就会明白了!题目要求:有一个字符串,里面有类似于分割符的“$”,现在要求输出字符串,
    遇到一个“$”则不管,当遇到“$$”的时候就输出一个空格!
    1,StringTokenizer st = new StringTokenizer(source,"$", true);
    2,增加boolean flag = false;用来判断两个连续的“$”
    String source ="2005$02$28$$Monday$true$false";
    StringTokenizer st = new StringTokenizer(source,"$", true);boolean flag = false;
    while (st.hasMoreElements()){
    String myString = st.nextToken();
    if (myString.equals("$")){
    if (flag){
    System.out.println(" ");
    }
    flag = true;
    continue;
    }
        System.out.println(myString);
        flag = false;
    }
      

  2.   

    或者更简单的方法就是,先把特殊分隔符用别的代替!
    例子如下:
    import java.lang.*;
    import java.lang.String;
    public class string 
    {
    public static void main (String [] args)
    {
    String source ="2005$02$28$$Monday$√$×";
    String source1;
    source1=source.replace('$','j');
    System.out.println(source1);
    String source2;
    source2=source1.replaceAll("jj"," ");
    System.out.print(source2);}
    }
      

  3.   

    Trailing empty strings are therefore not included in the resulting array. 
    空串的时候不被记录的下来的
      

  4.   

    StringTokenizer
    可以分割特殊字符
      

  5.   

    重写split()方法:
        public static String[] split(String input, String separator, int limit) {
            int index = 0;
            boolean matchLimited = limit > 0;
            ArrayList matchList = new ArrayList();
    //        Matcher m = new Matcher(this, input);
      Pattern p=Pattern.compile(separator);
      Matcher m=p.matcher(input);
            // Add segments before each match found
            while(m.find()) {
                if (!matchLimited || matchList.size() < limit - 1) {
                    String match = input.subSequence(index, m.start()).toString();
                    matchList.add(match);
                    index = m.end();
                } else if (matchList.size() == limit - 1) { // last one
                    String match = input.subSequence(index,
                                                     input.length()).toString();
                    matchList.add(match);
                    index = m.end();
                }
            }        // If no match was found, return this
            if (index == 0)
                return new String[] {input.toString()};        // Add remaining segment
            if (!matchLimited || matchList.size() < limit)
                matchList.add(input.subSequence(index, input.length()).toString());        // Construct result
            int resultSize = matchList.size();
    //        if (limit == 0)
    //            while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
    //                resultSize--;
            String[] result = new String[resultSize];
            return (String[])matchList.subList(0, resultSize).toArray(result);
        }String[] ary = test.split("\\,");换成:
    String[] ary = split(test, "\\,", 0);
    就可以了.
      

  6.   

    //        if (limit == 0)
    //            while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
    //                resultSize--;
    这段程序加上后就是原先那种效果.
      

  7.   

    花子的方法是正解。这种方法我是知道的,我就是想问如何用java的API来实现。
    我写的方法如下:
        private String[] split(String original,String regex){
            ArrayList ary = new ArrayList();
            String vlu = null;
            while(true){
                if(original.indexOf(regex) != -1){
                    vlu = original.substring(0, original.indexOf(regex));
                    ary.add(vlu);
                    original = original.substring(original.indexOf(regex) + 1);
                }else{
                    ary.add(original);
                    break;
                }
            }
            return (String[])ary.toArray(new String[0]);
        }