惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
B
Blog RSS Feed
美团技术团队
博客园 - 【当耐特】
C
Check Point Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
M
MIT News - Artificial intelligence
aimingoo的专栏
aimingoo的专栏
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 司徒正美
T
Tailwind CSS Blog
Last Week in AI
Last Week in AI
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
N
Netflix TechBlog - Medium
Vercel News
Vercel News
P
Proofpoint News Feed
IT之家
IT之家
I
InfoQ
腾讯CDC
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 飘飘雪

Redis 的 Rehash 操作详解 Panic 与 Crash 的区别 vscode常用快捷键大全 网络流量单位 Core文件作用、设置及用法 linux批量执行工具omnitty安装及使用 linux 下误改了/etc/profile下的文件path路径导致整个系统用不了命令 python安装 linux less和more命令用法 Tair中对热key的内部处理 Tair中对大key的内部处理方式 redis的redis-benchmark用法 redis的memtier-benchmark用法 阿里巴巴的缓存类测试产品及有缺点 如何在class文件中使用pom中profile级别的 <properties> profiles下的properties与properties有什么区别 什么是“黑天鹅”和“灰犀牛” java-sdk接口测试覆盖率统计实践 java应用接口自动化覆盖率统计实践
Guava 工具类之 Splitter的使用
飘飘雪 · 2023-11-06 · via 博客园 - 飘飘雪

Splitter可以对字符串进行分割,在分割时的方式有2种,

  1.按字符/字符串分割

  2.按正则进行分割

Splitter在分割完成时可以转换成listmap

一.按字符进行分割

//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());
}

转载于:https://www.cnblogs.com/MrRightZhao/p/11302831.html