split 分割 字符串(分隔符如:* ^ : | , .) 及注意点 (2013-01-05 16:41:41) 转载▼ var tag=′split,分隔符,java,android,it′;vartag_code='da48c4caf1ccab13c296628a57f0f915'; var rquotebligid=′b6487d470101g0hp′;varworldcup='0'; var $worldcupball='0'; 标签: split 分隔符 java android it 分类: Java 【1】单个符号作为分隔符 String address=”上海|上海市|闵行区|吴中路”; String[] splitAddress=address.split(“\\|”); //如果以竖线为分隔符,则split的时候需要加上两个斜杠【\\】进行转义 System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); String address=”上海*上海市*闵行区*吴中路”; String[] splitAddress=address.split(“\\*”); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); String address=”上海:上海市:闵行区:吴中路”; String[] splitAddress=address.split(“\\:”); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); String address=”上海.上海市.闵行区.吴中路”; String[] splitAddress=address.split(“\\.”); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); String address=”上海^上海市^闵行区^吴中路”; String[] splitAddress=address.split(“\\^”); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); String address=”上海@上海市@闵行区@吴中路”; String[] splitAddress=address.split(“@”); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); String address=”上海,上海市,闵行区,吴中路”; String[] splitAddress=address.split(“,”); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); 【2】多个符号作为分隔符 String address=”上海^上海市@闵行区#吴中路”; String[] splitAddress=address.split(“\\^|@|#”); System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); ***************************格式 小提示*************************************************** String address = new String(“192.168.13.240”); String[] str = address.split(“\\.”); for(String s : str){ System.out.println(s); } 输出格式: 192 168 13 240 System.out.println(splitAddress[0]+splitAddress[1]+splitAddress[2]+splitAddress[3]); 输出格式:上海上海市闵行区吴中路 ***************************************************************** 总结:(1)split表达式,其实就是一个正则表达式。* ^ | 等符号在正则表达式中属于一种有特殊含义的字符,如果使用此种字符作为分隔符,必须使用转义符即\\加以转义。 (2)如果使用多个分隔符则需要借助 | 符号,如【2】所示,但需要转义符的仍然要加上分隔符进行处理。