Guava 工具类之 Splitter的使用
Splitter可以对字符串进行分割,在分割时的方式有2种,
1.按字符/字符串分割
2.按正则进行分割
一.按字符进行分割
//1.用指定字符切分字符串,并转换成list
String s1 = "hello|hihi";
String s2 = "hello|haha|||";
Splitter.on("|").splitToList(s1).forEach(System.out::println);
Splitter.on("|").split(s1).forEach(item ->System.out.println(item)); //2.忽略掉空的字符串或者多余的分割符
Splitter.on("|").omitEmptyStrings().splitToList(s2).forEach(System.out::println); //3.忽略掉字符串中的空格
Splitter.on("|").omitEmptyStrings().trimResults().splitToList("hello | guava|||").forEach(System.out::println); //4.固定长度分割
Splitter.on("|").fixedLength(4).splitToList("aaaabbbbccccdddd").forEach(System.out::println); //5.指定长度分割
List<String> list = Splitter.on("#").limit(3).splitToList("a#b#c#d#e#"); //以#来分割,分3部分成 a b #c#d#e 3部分
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.get(2));
二.按正则来进行分割
//1.传入字符的分割
Splitter.onPattern("\\|").splitToList("hello|world").forEach(System.out::println); //2.传入pattern的分割
Splitter.on(Pattern.compile("\\|")).omitEmptyStrings().trimResults().splitToList("a|b|c||").forEach(System.out::println);
//3.传入pattern 转换成map
Map<String, String> map = Splitter.on(Pattern.compile("\\|")).omitEmptyStrings()
.trimResults().withKeyValueSeparator("=").split("a=b|c=d");
for (Entry<String, String> entry : map.entrySet()) {
System.out.println(entry.getKey() +" = "+ entry.getValue());
}
三.测试
/**
* list转换为字符串
*/
@Test
public void joinTest(){
List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");
String result = Joiner.on(",").join(names); assertEquals(result, "John,Jane,Adam,Tom");
} /**
* map转换为字符串
*/
@Test
public void whenConvertMapToString_thenConverted() {
Map<String, Integer> salary = Maps.newHashMap();
salary.put("John", 1000);
salary.put("Jane", 1500);
String result = Joiner.on(" , ").withKeyValueSeparator(" = ")
.join(salary);
System.out.println(result);
} /**
* list转String,跳过null
*/
@Test
public void whenConvertListToStringAndSkipNull_thenConverted() {
List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
String result = Joiner.on(",").skipNulls().join(names);
System.out.println(result);
assertEquals(result, "John,Jane,Adam,Tom");
} /**
* list转String,将null变成其他值
*/
@Test
public void whenUseForNull_thenUsed() {
List<String> names = Lists.newArrayList("John", null, "Jane", "Adam", "Tom");
String result = Joiner.on(",").useForNull("nameless").join(names);
System.out.println(result);
assertEquals(result, "John,nameless,Jane,Adam,Tom");
} /**
* String to List
*/
@Test
public void whenCreateListFromString_thenCreated() {
String input = "apple - banana - orange";
List<String> result = Splitter.on("-").trimResults().splitToList(input);
System.out.println(result);
//assertThat(result, contains("apple", "banana", "orange"));
} /**
* String to Map
*/
@Test
public void whenCreateMapFromString_thenCreated() {
String input = "John=first,Adam=second";
Map<String, String> result = Splitter.on(",")
.withKeyValueSeparator("=")
.split(input); assertEquals("first", result.get("John"));
assertEquals("second", result.get("Adam"));
} /**
* 多个字符进行分割
*/
@Test
public void whenSplitStringOnMultipleSeparator_thenSplit() {
String input = "apple.banana,,orange,,.";
List<String> result = Splitter.onPattern("[.|,]")
.omitEmptyStrings()
.splitToList(input);
System.out.println(result);
} /**
* 每隔多少字符进行分割
*/
@Test
public void whenSplitStringOnSpecificLength_thenSplit() {
String input = "Hello world";
List<String> result = Splitter.fixedLength(3).splitToList(input);
System.out.println(result);
} /**
* 限制分割多少字后停止
*/
@Test
public void whenLimitSplitting_thenLimited() {
String input = "a,b,c,d,e";
List<String> result = Splitter.on(",")
.limit(4)
.splitToList(input); assertEquals(4, result.size());
System.out.println(result);
}
Guava 工具类之 Splitter的使用的更多相关文章
- 强大的 Guava 工具类
Java 开发的同学应该都使用或者听说过 Google 提供的 Guava 工具包.日常使用最多的肯定是集合相关的工具类,还有 Guava cache,除了这些之外 Guava 还提供了很多有用的功能 ...
- 工具篇:介绍几个好用的guava工具类
前言 平时我们都会封装一些处理缓存或其他的小工具.但每个人都封装一次,重复造轮子,有点费时间.有没有一些好的工具库推荐-guava.guava是谷歌基于java封装好的开源库,它的性能.实用性,比我们 ...
- Google的Guava工具类splitter和apache stringutil对比 编辑
一直用的是apache的stringutil工具类,其实google的工具类项目 guava中居然也有字符串的分隔类splitter的,在 http://code.google.com/p/guava ...
- Guava工具类
原文链接:http://blog.csdn.net/mnmlist/article/details/53425865 Objects类 Objects类有几个比较不错的方法,toString.hash ...
- Google guava工具类的介绍和使用
概述 工具类 就是封装平常用的方法,不需要你重复造轮子,节省开发人员时间,提高工作效率.谷歌作为大公司,当然会从日常的工作中提取中很多高效率的方法出来.所以就诞生了guava.. 高效设计良好的API ...
- Guava工具类学习
目录 一.介绍 二.Optional类 1.定义 2.java8自带Optional 3.使用 三.Preconditions类 1.定义 2.使用 四.Ordering类 1.定义 2.使用 五.R ...
- Guava 工具类之Cache的使用
一.guava cache 介绍 1.介绍 guava cache是Google guava中提供的一款轻量级的本地缓存组件,其特点是简单.轻便.完善.扩展性强,内存管理机制也相对完善. 2.使用缓存 ...
- Guava 工具类之joiner的使用
joiner主要用于对字符串的连接,也可用于对map中key value的连接 public class JoinerTest { private static final List<Strin ...
- Guava 工具类之Strings 的使用
public class StringTest { public static void main(String[] args) { //判断是null还是空字符串 boolean b1 = Stri ...
随机推荐
- 023_统计当前 Linux 系统中可以登录计算机的账户有多少个
#!/bin/bash #方法 1: grep "bash$" /etc/passwd | wc -l #方法 2: #-F END都要大写! awk -F: '/bash$/{x ...
- python define function
>>> def square(x): ... 'calculates the square of the number x.' ... return x*x ... >> ...
- cdh版hbase构建Phoenix 遇到的坑
Phoenix 构建cdh版hbase遇到的坑 1. 安装phoenix 下载:在github上下载对应版本https://github.com/apache/phoenix 解压:略 编译: 修改根 ...
- layer 漂亮的弹窗
layer.confirm('<font color="red">请认真核对账目信息,提交后将不可撤回!!</font>', { icon:3, title ...
- 方阵转置(c++)
#include #include using namespace std; int main(int argc,char* argv[]) { int a[4][4]={ {0,1,2,3}, {4 ...
- javaEE项目部署方式
1.手动部署 2.自动化部署 “自动化”的具体体现:向版本库提交新的代码后,应运服务器上自动部署
- 【零基础】AI神经元解析(含实例代码)
一.序言 关于“深度学习”大部分文章讲的都云里雾里,直到看到“床长”的系列教程以及<深度学习入门:基于Python的理论与实现>,这里主要是对这两个教程进行个人化的总结,目标是让“0基础” ...
- StringUtils的isNotEmpty,isNotBlank方法的区别
这两个用着用着老是混淆或者忘记,今天写一下做个笔记,对比下两个判断方法的区别 isNotEmpty: 判断某字符串是否非空,等于!isEmpty(String str),这里不能排除空格字符 Stri ...
- CLR 虚方法调用和接口方法调用
不知接口方法和虚方法分发有什么区别?似乎在CIL中都是callvirt指令. 对,MSIL里都是callvirt,但JIT的时候得到了不同的处理:对虚方法的分发是编译成这样: mov ecx, es ...
- java中json的使用和解析
1.创建json对象 1.1 创建JSONObject对象 使用map初始化json @Test public void test1() { Map<String, Object> map ...