Java之spilt()函数

一、单个符号作为分隔符 

package com.regix;

public class FuncSpilt {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String address = "上海|上海市|闵行区|吴中路";
        //String[] splitAddress=address.split("\\|"); 
        String splitAddress[]=address.split("\\|"); 
        System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]+splitAddress.length);
    }

}

二、多个符号作为分隔符 

package com.regix;

public class FuncSpilt {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         String address="上海^上海市@闵行区#吴中路";
        String[] splitAddress=address.split("\\^|@|#");
        System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]+splitAddress.length);
    }

}

总结:

     (1)split表达式,其实就是一个正则表达式。*  ^ | 等符号在正则表达式中属于一种有特殊含义的字符,如果使用此种字符作为分隔符,必须使用转义符即\\加以转义;
          (2)如果使用多个分隔符则需要借助 | 符号,如二所示,但需要转义符的仍然要加上分隔符进行处理。

猜你喜欢

转载自www.cnblogs.com/iloverain/p/9004648.html