Google Guava的splitter,分割字符串的用法

package com.sxd.swapping.guava;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import org.assertj.core.util.Lists;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; /**
* @Author: SXD
* @Description: Google Guava的splitter,分割字符串的用法
* @Date: create in 2019/11/12 9:52
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class GuavaSplitterTest { String str = "a,,b, c,,,,2312312342,dasdaaa ,德玛西亚, 艾欧尼亚,寒冰之地 ,abCDefGHdasIJklmnoPpQqZz,ooAAdas"; /**
* 按 指定字符 拆分源字符串
*/
@Test
public void splitTest(){
Iterable<String> split = Splitter.on(",").split(str);
printResult(split);
} /**
* 按 指定字符 拆分源字符串
* 并去除 空值
*/
@Test
public void omitEmptyStringsTest(){
Iterable<String> split = Splitter.on(",").omitEmptyStrings().split(str);
printResult(split);
} /**
* 按 指定字符 拆分源字符串
* 并去除空值
* 并去除额外空格
*/
@Test
public void trimResultsTest(){
Iterable<String> split = Splitter.on(",").trimResults().omitEmptyStrings().split(str);
printResult(split);
} /**
* 按 指定字符 拆分源字符串
* 并去除空值
* 并 按照指定类型 去除每一个分隔元素内的 指定类型
*/
@Test
public void trimResultsWithCharTest(){
//去除 元素中包含的数字
Iterable<String> split1 = Splitter.on(",").trimResults(CharMatcher.digit()).omitEmptyStrings().split(str);
printResult(split1); //去除 元素中的 空格
Iterable<String> split2 = Splitter.on(",").trimResults(CharMatcher.whitespace()).omitEmptyStrings().split(str);
printResult(split2); //去除 元素中的 空格
Iterable<String> split3 = Splitter.on(",").trimResults(CharMatcher.breakingWhitespace()).omitEmptyStrings().split(str);
printResult(split3); //去除 元素中的 包含在ASCII中的所有元素 [留下的:例如中文]
Iterable<String> split4 = Splitter.on(",").trimResults(CharMatcher.ascii()).omitEmptyStrings().split(str);
printResult(split4); //去除 元素中的 任何元素
Iterable<String> split5 = Splitter.on(",").trimResults(CharMatcher.any()).omitEmptyStrings().split(str);
printResult(split5); //不去除 元素中的 任何元素
Iterable<String> split6 = Splitter.on(",").trimResults(CharMatcher.none()).omitEmptyStrings().split(str);
printResult(split6); //去除 元素左右两侧的在a-z范围内的元素
Iterable<String> split9 = Splitter.on(",").trimResults(CharMatcher.inRange('a','z')).omitEmptyStrings().split(str);
printResult(split9); //去除 元素左右两侧中不包含a 的元素
Iterable<String> split10 = Splitter.on(",").trimResults(CharMatcher.isNot('a')).omitEmptyStrings().split(str);
printResult(split10); } /**
* 将 Iterable<String> 转化为 List<String>
*
* 最终打印结果集
* @param split Iterable<String>
*/
private void printResult(Iterable<String> split){
ArrayList<String> result = Lists.newArrayList(split);
System.out.println("结果集大小:"+result.size());
System.out.println(">>>>输入结果集:");
for (String s : result) {
System.out.println(s);
}
System.out.println();
}
}

效果可以自行 粘贴 ,单元测试直接运行即可。

【java】【guava】Google Guava的splitter用法的更多相关文章

  1. 使用 Google Guava 美化你的 Java 代码

    文章转载自:http://my.oschina.net/leejun2005/blog/172328 目录:[ - ] 1-使用 GOOGLE COLLECTIONS,GUAVA,STATIC IMP ...

  2. Google的java工具类Guava

    前言 google开发java项目肯定也不想重复造轮子,所以肯定也有工具类,就是它了:Guava 我将举例几个实际的例子,发挥这个工具类好用的功能.更多的方法和功能,还有内部的实现可以直接参考http ...

  3. Google Java编程库Guava介绍

    本系列想介绍下Java下开源的优秀编程库--Guava[ˈgwɑːvə].它包含了Google在Java项目中使用一些核心库,包含集合(Collections),缓存(Caching),并发编程库(C ...

  4. String split方法与Guava Splitter用法区别

    String split方法与Guava Splitter用法区别 今天同事写了一段使用String split方法的代码,如下所示,同事期望得到的是字符"1",但是没想到却得到空 ...

  5. 使用 Google Guava 美化你的 Java 代码:1~4 【转】

    文章转载自:http://my.oschina.net/leejun2005/blog/172328 1.使用Google Collections,Guava,static imports编写漂亮代码 ...

  6. [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具

    原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...

  7. Java内存缓存-通过Google Guava创建缓存

    谷歌Guava缓存 Guava介绍 Guava是Google guava中的一个内存缓存模块,用于将数据缓存到JVM内存中.实际项目开发中经常将一些公共或者常用的数据缓存起来方便快速访问. Guava ...

  8. java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)

    学习参考文章: http://blog.csdn.net/wisgood/article/details/13297535 http://ifeve.com/google-guava/ http:// ...

  9. 【转载】使用 Google Guava 美化你的 Java 代码

    转载地址: https://blog.csdn.net/wisgood/article/details/13297535 原文地址:https://my.oschina.net/leejun2005/ ...

随机推荐

  1. OpenGL光照2:材质和光照贴图

    本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/ 非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程 的内容为插入注释,可以先跳过 ...

  2. MySQL(6)---变量

    MySQL(6)-变量 这里学习变量主要是为后面学习存储过程和函数做铺垫. 变量的分类 系统变量: 全局变量 会话变量 自定义变量: 用户变量 局部变量 一.系统变量 1.概述 说明:变量由系统定义, ...

  3. SPA项目开发之首页导航+左侧菜单

    Mock.js: 前后端分离之后,前端迫切需要一种机制,不再需要依赖后端接口开发,而mockjs就可以做到这一点 Mock.js是一个模拟数据的生成器,用来帮助前端调试开发.进行前后端的原型分离以及用 ...

  4. E203数据冲突处理OITF

    流水线的数据冲突分为三类:WAR,RAW,WAW https://wenku.baidu.com/view/e066926d48d7c1c708a14508.html WAR: write after ...

  5. rsync 未授权访问漏洞

    rsync rsync,remote synchronize顾名思意就知道它是一款实现远程同步功能的软件,它在同步文件的同时,可以保持原来文件的权限.时间.软硬链接等附加信息. rsync是用 &qu ...

  6. Windows下Python 3.6 安装BeautifulSoup库

    - - 下载安装 安装方法如下: 到 - PIP安装 如果上一种方法安装不成功,可以用第二种方法,记住,Python3.6下是pip3. 安装方法如下: pip3 install beautifuls ...

  7. 设计模式 - Java中单例模式的6种写法及优缺点对比

    目录 1 为什么要用单例模式 1.1 什么是单例模式 1.2 单例模式的思路和优势 2 写法① - 饥饿模式 2.1 代码示例 2.2 优缺点比较 3 写法② - 懒惰模式 3.1 代码示例 3.2 ...

  8. SQL学习_SELECT

    查询列: SQL:SELECT name FROM heros 多列查询: SQL:SELECT name, hp_max, mp_max, attack_max, defense_max FROM ...

  9. [b0027] python 归纳 (十二)_并发队列Queue的使用

    # -*- coding: UTF-8 -*- """ 学习队列 Queue 总结: 1. 队列可以设置大小,也可以无限大小 2. 空了,满了,读写时可以阻塞,也可以报错 ...

  10. ubuntu 查看软件包中的内容 (已经安装)

    在 使用 apt 进行安装软件的时候,我们要经常判断,软件安装了什么和安装到什么地方.这时候 我们要使用 dpkg -L 命令来进行查看: 同样 在 fedora 上可以使用 rpm -ql iper ...