1|202|27|0730|07302088169|07302088169|20070705 121212||20100801 000000|201000831 235959||||||4|0|500||2|||||||||
现需要将|前面的数据截取出来,如果是空则不要。求高手帮忙

解决方案 »

  1.   

    先搞个split函数,在用这个方法得到String数组    /**
         * 
         * 自定义字符串分割函数,返回数组 -- by PanJun
         * 
         * @param s
         *            待分解串
         * @param token
         *            分解标识
         * @return
         * 
         */
        public static String[] split(String s, String token) {
            if (s == null)
                return null;        if (token == null || s.length() == 0)
                return new String[] { s };        int size = 0;
            String[] result = new String[4];
            while (s.length() > 0) {
                int index = s.indexOf(token);
                String splitOne = s;
                if (index > -1) {
                    splitOne = s.substring(0, index);
                    s = s.substring(index + token.length());
                } else {
                    s = "";
                }            if (size >= result.length) {
                    String[] tmp = new String[result.length * 2];
                    System.arraycopy(result, 0, tmp, 0, result.length);
                    result = tmp;
                }            if (splitOne.length() > 0) {
                    result[size++] = splitOne;
                }
            }        String[] tmp = result;
            result = new String[size];
            System.arraycopy(tmp, 0, result, 0, size);
            return result;
        }
      

  2.   


    public static void main(String[] args){
    String str="1|202|27|0730|07302088169|07302088169|20070705 121212||20100801 000000|201000831 235959||||||4|0|500||2|||||||||";
    String[] arr=str.split("(?<!^)\\|+");
    for(String s:arr)
    System.out.println(s); }
      

  3.   


    String str="1|202|27|0730|07302088169|07302088169|20070705 121212||20100801 000000|201000831 235959||||||4|0|500||2|||||||||";
    String[] arr=str.split("\\|+");
    for(String s:arr)
    System.out.println(s);不用加(?<!^)也可以。要是字符创以|开头的就要加(?<!^)
      

  4.   


    使用split函数 一般的这种题目都是这么写的