我们平时进行简单的字符串分割的时候,尽量不要用String自身的split方法,它是匹配正则表达式的,如果遇到$这种特殊字符,需要转义一下。用StringUtils.split()方法会更方便

使用apache StringUtils.split替代String.split
如果你对下面几个结果有疑惑的话,建议使用apache commons包的StringUtils.split来替代。

String[] strs = "".split(",");  

结果是strs.length=1,strs[0]=""

String[] strs = ",".split(",");  

结果是strs.length=0

String[] strs = ",1,".split(",");  

结果是strs.length=2,strs[0]="",strs[1]="1"

String[] strs = StringUtils.split(",1,,2,", ",");  

结果是strs.length=2,strs[0]="1",strs[1]="2"

 System.out.println(":ab:cd:ef::".split(":").length);//末尾分隔符全部忽略
System.out.println(":ab:cd:ef::".split(":",-1).length);//不忽略任何一个分隔符
System.out.println(StringUtils.split(":ab:cd:ef::",":").length);//最前面的和末尾的分隔符全部都忽略,apache commons
System.out.println(StringUtils.splitPreserveAllTokens(":ab:cd:ef::",":").length);//不忽略任何一个分隔符 apache commons
输出:
4
6
3
6
String s =",1,,2,3,4,,";
String[] split1 = s.split(",");
String[] split2 = StringUtils.splitString(s, ",");

调试结果:

总结: 
String.split()会包含空字符串,而且是包含 头部的和中间的, 不包含有效数字后面所有的空字符串. 
StringUtils.split()会过滤所有的空字符串. 当然空格不会被过滤.

String中的split(",")和split(",",-1)的区别:
 
1、当字符串最后一位有值时,两者没有区别
 
2、当字符串最后一位或者N位是分隔符时,前者不会继续切分,而后者继续切分。即前者不保留null值,后者保留。
package stringsplit;
public class stringSplit {
public static void main(String[] args) {
String line = "hello,,world,,,";
String res1[] = line.split(",");
String res2[] = line.split(",", -1);
int i=1;
for(String r: res1)
System.out.println(i+++r);
System.out.println("========================");
i=1;
for(String r: res2)
System.out.println(i+++r);
}
}

输出结果是:

1hello
2
3world
========================
1hello
2
3world
4
5
6

String.split()与StringUtils.split()的更多相关文章

  1. String.split()与StringUtils.split()的区别

    import com.sun.deploy.util.StringUtils; String s =",1,,2,3,4,,"; String[] split1 = s.split ...

  2. StringUtils.split()和string.split()的区别

    场景 出于业务考虑,将多个字符串拼接起来时,使用的分隔符是;,;.如果要将这样一个拼接来的字符串分割成原本的多个字符串时,就需要使用到jdk自带的split()方法.不过因为公司的编程规范,改为使用了 ...

  3. 使用 StringUtils.split 的坑

    点赞再看,动力无限. 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 未读代码博客 已经收录,有很多知识点和系列文章. 在日常的 Java 开发中,由于 J ...

  4. C# Split的用法,Split分割字符串

    C# Split的用法,Split分割字符串 分割单个字串:string str="来自张三的亲切问候!;string[] strarry=str.Split(new string[] { ...

  5. Python: str.split()和re.split()的区别

    str.split() 单一分隔符,使用str.split()即可 str.split不支持正则及多个切割符号,不感知空格的数量 re.split() 多个分隔符,复杂的分隔情况,使用re.split ...

  6. .split(",", -1);和.split(",")的区别

    .split(",", -1);和.split(",")的区别在于://eg:String a="河南省,,金水区".//a.split(& ...

  7. python split(),os.path.split()和os.path.splitext()函数用法

    https://blog.csdn.net/T1243_3/article/details/80170006   # -*- coding:utf-8 -*- """ @ ...

  8. split(),strip,split("/")[-1] 和 split("/",-1)的区别

    Python中split()函数,通常用于将字符串切片并转换为列表. 一.函数说明: split():语法: str.split(str="",num=string.count(s ...

  9. str.split()与re.split()的区别

    str.split(): >>>'hello, world'.split() >>>['hello,','world'] >>>'hello, w ...

随机推荐

  1. An error occurred. Sorry, the page you are looking for is currently unavailable. Please try again later.

    刚装完 PHP.Nginx,准备跑下 phpMyAdmin 程序,结果报以下错误: An error occurred. Sorry, the page you are looking for is ...

  2. java获取当前网站的IP地址

    package ip; import java.net.InetAddress; import java.net.UnknownHostException; /** * * @author * */ ...

  3. STL next_permutation 算法原理和自行实现

    目标 STL中的next_permutation 函数和 prev_permutation 两个函数提供了对于一个特定排列P,求出其后一个排列P+1和前一个排列P-1的功能. 这里我们以next_pe ...

  4. upstream模块介绍

    upstream模块介绍 Nginx的负载均衡功能依赖于ngx_http_upsteam_module模块,所支持的代理方式包括proxy_pass.fastcgi_pass.memcached_pa ...

  5. 东大oj1155 等凹函数

    Problem Description 定义一种数字称为等凹数字,即从高位到低位,每一位的数字先递减再递增,且该数是一个回文数,即从左读到右与从右读到左是一样的,仅形成一个等凹峰,如543212345 ...

  6. AngularJS资源合集[备忘]【申明:来源于网络】

    AngularJS资源合集[备忘][申明:来源于网络] 地址:http://blog.csdn.net/allgis/article/details/44646597

  7. Android启动页欢迎界面大全 (网址)

    地址:http://download.csdn.net/detail/u013424496/9539810

  8. openERP笔记,自定义开发模块

    ##目标 OpenERP模块基本结构 使用模块添加额外的字段(Date Required和Rush Order) 扩展视图, 让OpenERP能够显示新的字段 修改用于OpenERP工作流的可用状态 ...

  9. 关于STM32时钟系统

    初学STM32,感觉最蛋疼的是它的时钟系统,每次看到它的那个时钟树就有点晕,虽然看了很多这方面的资料,甚至也已经写过很多STM32的模块代码,做过一些小项目,但一直还是对这一块模模糊糊,似懂非懂,所以 ...

  10. vue开发记录--通用时间格式函数

    parseTime(time, fm) { // 解析时间 time: 时间戳或者实践对象 fm: 格式 默认是{y}-{m}-{d} {h}:{i}:{s} if (arguments.length ...