split切割字符串注意事项
2017-09-11 17:20
267 查看
split()方法的源码:
public String[] split(String regex, int limit) { /* fastpath if the regex is a (1)one-char String and this character is not one of the RegEx's meta characters ".$|()[{^?*+\\", or (2)two-char String and the first char is the backslash and the second is not the ascii digit or ascii letter. */ char ch = 0; if (((regex.value.length == 1 && ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) || (regex.length() == 2 && regex.charAt(0) == '\\' && (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 && ((ch-'a')|('z'-ch)) < 0 && ((ch-'A')|('Z'-ch)) < 0)) && (ch < Character.MIN_HIGH_SURROGATE || ch > Character.MAX_LOW_SURROGATE)) { int off = 0; int next = 0; boolean limited = limit > 0; ArrayList<String> list = new ArrayList<>(); while ((next = indexOf(ch, off)) != -1) { if (!limited || list.size() < limit - 1) { list.add(substring(off, next)); off = next + 1; } else { // last one //assert (list.size() == limit - 1); list.add(substring(off, value.length)); off = value.length; break; } } // If no match was found, return this if (off == 0) return new String[]{this}; // Add remaining segment if (!limited || list.size() < limit) list.add(substring(off, value.length)); // Construct result int resultSize = list.size(); if (limit == 0) while (resultSize > 0 && list.get(resultSize - 1).length() == 0) resultSize--; String[] result = new String[resultSize]; return list.subList(0, resultSize).toArray(result); } return Pattern.compile(regex).split(this, limit); }从源码中可以看出,split()方法是正则表达式,所以如果要使用到以下字符“.$|()[{^?*+\\”作为切割符的话,需要进行转义,因为这些字符在正则表达式中属于一种有特殊含义的字符。
*********************************************************************************************************************************************************
【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】所示,但需要转义符的仍然要加上分隔符进行处理。
相关文章推荐
- string.Split方法来分割字符串的注意事项:
- java使用split切割字符串的时候,注意转义字符
- java split分割字符串注意事项
- java 字符串 Split切割 特殊字符(例:* ^ : | )注意点
- 王立平--split字符串切割
- Android通过http方式获取JSON字符串并解析的注意事项(乱码,小黑框)
- java中使用split方法切割字符串无效进行时
- java split(".");注意事项
- Java split()方法实现切割字符串
- python字符串切割:str.split()和re.split()对比
- 将JSON格式字符串转成对象、数组、取值等(注意事项)
- Android翻译多国语言字符串注意事项 01
- split 分割 字符串(分隔符如:* ^ : | , .) 及注意点
- JavaScript基础 split() 化整为零 将一个字符串切割成若干段(不限制段的数量) 返回一个数组
- sqlserver中时间格式的字符串传递时的注意事项
- 关于本地缓存localstorage与sessionStorage 数组 (array)字符串(string) 对象(object)的存储技巧和注意事项
- Android通过http方式获取JSON字符串并解析的注意事项(乱码,小黑框)
- c# 字符串切割 split
- 关于本地缓存localstorage与sessionStorage 数组 (array)字符串(string) 对象(object)的存储技巧和注意事项
- String之 split 分割字符串(分隔符如:* ^ : | , .) 及注意点