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

  1. package com.sxd.swapping.guava;
  2.  
  3. import com.google.common.base.CharMatcher;
  4. import com.google.common.base.Splitter;
  5. import org.assertj.core.util.Lists;
  6. import org.junit.Test;
  7. import org.junit.runner.RunWith;
  8. import org.springframework.boot.test.context.SpringBootTest;
  9. import org.springframework.test.context.junit4.SpringRunner;
  10.  
  11. import java.util.ArrayList;
  12.  
  13. /**
  14. * @Author: SXD
  15. * @Description: Google Guava的splitter,分割字符串的用法
  16. * @Date: create in 2019/11/12 9:52
  17. */
  18. @RunWith(SpringRunner.class)
  19. @SpringBootTest
  20. public class GuavaSplitterTest {
  21.  
  22. String str = "a,,b, c,,,,2312312342,dasdaaa ,德玛西亚, 艾欧尼亚,寒冰之地 ,abCDefGHdasIJklmnoPpQqZz,ooAAdas";
  23.  
  24. /**
  25. * 按 指定字符 拆分源字符串
  26. */
  27. @Test
  28. public void splitTest(){
  29. Iterable<String> split = Splitter.on(",").split(str);
  30. printResult(split);
  31. }
  32.  
  33. /**
  34. * 按 指定字符 拆分源字符串
  35. * 并去除 空值
  36. */
  37. @Test
  38. public void omitEmptyStringsTest(){
  39. Iterable<String> split = Splitter.on(",").omitEmptyStrings().split(str);
  40. printResult(split);
  41. }
  42.  
  43. /**
  44. * 按 指定字符 拆分源字符串
  45. * 并去除空值
  46. * 并去除额外空格
  47. */
  48. @Test
  49. public void trimResultsTest(){
  50. Iterable<String> split = Splitter.on(",").trimResults().omitEmptyStrings().split(str);
  51. printResult(split);
  52. }
  53.  
  54. /**
  55. * 按 指定字符 拆分源字符串
  56. * 并去除空值
  57. * 并 按照指定类型 去除每一个分隔元素内的 指定类型
  58. */
  59. @Test
  60. public void trimResultsWithCharTest(){
  61. //去除 元素中包含的数字
  62. Iterable<String> split1 = Splitter.on(",").trimResults(CharMatcher.digit()).omitEmptyStrings().split(str);
  63. printResult(split1);
  64.  
  65. //去除 元素中的 空格
  66. Iterable<String> split2 = Splitter.on(",").trimResults(CharMatcher.whitespace()).omitEmptyStrings().split(str);
  67. printResult(split2);
  68.  
  69. //去除 元素中的 空格
  70. Iterable<String> split3 = Splitter.on(",").trimResults(CharMatcher.breakingWhitespace()).omitEmptyStrings().split(str);
  71. printResult(split3);
  72.  
  73. //去除 元素中的 包含在ASCII中的所有元素 [留下的:例如中文]
  74. Iterable<String> split4 = Splitter.on(",").trimResults(CharMatcher.ascii()).omitEmptyStrings().split(str);
  75. printResult(split4);
  76.  
  77. //去除 元素中的 任何元素
  78. Iterable<String> split5 = Splitter.on(",").trimResults(CharMatcher.any()).omitEmptyStrings().split(str);
  79. printResult(split5);
  80.  
  81. //不去除 元素中的 任何元素
  82. Iterable<String> split6 = Splitter.on(",").trimResults(CharMatcher.none()).omitEmptyStrings().split(str);
  83. printResult(split6);
  84.  
  85. //去除 元素左右两侧的在a-z范围内的元素
  86. Iterable<String> split9 = Splitter.on(",").trimResults(CharMatcher.inRange('a','z')).omitEmptyStrings().split(str);
  87. printResult(split9);
  88.  
  89. //去除 元素左右两侧中不包含a 的元素
  90. Iterable<String> split10 = Splitter.on(",").trimResults(CharMatcher.isNot('a')).omitEmptyStrings().split(str);
  91. printResult(split10);
  92.  
  93. }
  94.  
  95. /**
  96. * 将 Iterable<String> 转化为 List<String>
  97. *
  98. * 最终打印结果集
  99. * @param split Iterable<String>
  100. */
  101. private void printResult(Iterable<String> split){
  102. ArrayList<String> result = Lists.newArrayList(split);
  103. System.out.println("结果集大小:"+result.size());
  104. System.out.println(">>>>输入结果集:");
  105. for (String s : result) {
  106. System.out.println(s);
  107. }
  108. System.out.println();
  109. }
  110. }

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

【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. CAD打印图纸要怎么操作?简单方法分享给你

    大家日常生活中多多少少的都接触到过CAD文件,CAD图是借助CAD制图软件来进行绘制完成的.唯一的困惑就是CAD图纸的格式大多数均为dwg格式的,查看起来不是那么的方便?所以很多设计师们都会选择将图纸 ...

  2. GO基础之函数

    一.Go语言函数的格式 函数构成了代码执行的逻辑结构,在Go语言中,函数的基本组成为:关键字 func.函数名.参数列表.返回值.函数体和返回语句,每一个程序都包含很多的函数,函数是基本的代码块. 函 ...

  3. Taro聊天室|react+taro仿微信聊天App界面|taro聊天实例

    一.项目简述 taro-chatroom是基于Taro多端实例聊天项目,运用Taro+react+react-redux+taroPop+react-native等技术开发的仿微信App界面聊天室,实 ...

  4. java中字符串String、StringBuilder、StringBuffer的常用方法

    String的常用方法: public static void main(String[] args) { String str = "Hello world!"; // 获取字符 ...

  5. 告诉你一些DBA求职面试技巧

    告诉你一些DBA求职面试技巧 要自信!永远不要低估你的能力.如果你不了解什么问题的答案,承认它.重点放在你找出答案的能力和你学习的意愿. 不要自大!是的,你可能过于自信而被认为是骄傲的.轻率的,甚至是 ...

  6. VSCode 如何同步设置

    微软新推出的 VSCode 是一款开源.轻量.良心的开发工具,一经问世,迅速受到全球广泛开发者的好评与青睐,威风之下有干掉 Sublime Text 的趋势.然而有不少 VSCode 使用者吐槽其不能 ...

  7. 挂载system.img并提取文件

    今天提取线刷包的system.img出来,使用Mount命令挂载 $ sudo mount -t ext4 -o loop system.img /mnt mount: 文件系统类型错误.选项错误./ ...

  8. itest(爱测试) 4.2.0 发布,开源BUG 跟踪管理 & 敏捷测试管理软件

    itest 入选 2019 年度最受欢迎开源中国软件 开源工具的发展,离不开你我的支持,需要您投上宝贵的一票  去投票 v4.2.0下载地址 :itest下载 itest 简介:查看简介 itest ...

  9. vue操作select获取option值

    如何实时的获取你选中的值 只用@change件事 @change="changeProduct($event)" 动态传递参数 vue操作select获取option的ID值 如果 ...

  10. bootstrap如何去除自带的样式----导航栏中的菜单实现平滑的过渡到对应的菜单区域-------动态跟换模态框中的内容

    问题1:如何去除bootstap中css中自带的overflow:hidden这个样式 今天遇见在bootstap中轮播图上的  附带图  片不能够显示出来,图片始终有一部分的高度  被隐藏了 后来通 ...