我们平时进行简单的字符串分割的时候,尽量不要用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. Golang, 以 9 个简短代码片段,弄懂 defer 的使用特点

    作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:http://www.cnblogs.com/linguan ...

  2. Ubuntu系统安装nginx

    1.首先查看linux系统 cat /proc/version Linux version 4.9.59-v7+ (dc4@dc4-XPS13-9333) (gcc version 4.9.3 (cr ...

  3. Nestjs 获取cookie

    Docs yarn add cookie-parser main.ts import { NestFactory } from '@nestjs/core'; import { AppModule } ...

  4. Java之JVM监控工具分享

    Java之JVM监控工具分享 JVM的基本知识常用的也就是类加载机制,内存区域.分配.OOM,GC,JVM参数调优 几个链接自己看: 内存区域&类加载机制 分配策略&垃圾回收算法.收集 ...

  5. 初识NLTK

    需要用处理英文文本,于是用到python中nltk这个包 f = open(r"D:\Postgraduate\Python\Python爬取美国商标局专利\s_exp.txt") ...

  6. python使用matplotlib绘制折线图教程

    Matplotlib是一个Python工具箱,用于科学计算的数据可视化.借助它,Python可以绘制如Matlab和Octave多种多样的数据图形.下面这篇文章主要介绍了python使用matplot ...

  7. C#常用加密方法

    using System; using System.IO; using System.Security.Cryptography; using System.Text; /// <summar ...

  8. mysql8.0发布新特性

    2018年4月21日 14:36:42 https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-11.html#mysqld-8-0-11-b ...

  9. python的var_dump,打印对象内容

    from __future__ import print_function from types import NoneType __author__ = "Shamim Hasnath&q ...

  10. Linux之grep的使用

    基本介绍 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...