guava的String之Splitter
1.常用方法
Base Factories
Method | Description | Example |
Splitter.on(char) | Split on occurrences of a specific, individual character. | Splitter.on(';') |
Splitter.on(CharMatcher) | Split on occurrences of any character in some category. |
Splitter.on(CharMatcher.BREAKING_WHITESPACE) Splitter.on(CharMatcher.anyOf(";,.")) |
Splitter.on(String) | Split on a literal String. | Splitter.on(", ") |
Splitter.on(Pattern) Splitter.onPattern(String) |
Split on a regular expression. | Splitter.onPattern("\r?\n") |
Splitter.fixedLength(int) | Splits strings into substrings of the specified fixed length. The last piece can be smaller than length, but will never be empty. | Splitter.fixedLength(3) |
Modifiers
Method | Description | Example |
omitEmptyStrings() | Automatically omits empty strings from the result. |
Splitter.on(',').omitEmptyStrings(). split("a,,c,d") returns "a", "c", "d" |
trimResults() | Trims whitespace from the results; equivalent totrimResults(CharMatcher.WHITESPACE). |
Splitter.on(',').trimResults().split ("a, b, c, d") returns "a", "b", "c", "d" |
trimResults(CharMatcher) | Trims characters matching the specified CharMatcher from results. |
Splitter.on(',').trimResults (CharMatcher.is('_')).split("_a ,_b_ ,c__") returns "a ", "b_ ", "c". |
limit(int) | Stops splitting after the specified number of strings have been returned. |
Splitter.on(',').limit(3).split ("a,b,c,d") returns "a", "b", "c,d" |
2.实例
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
package string;
import java.util.Map;
import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
public class SplitterUse {
public static void useMethod() {
//根据分隔符进行分割
String sequence="a,,-b, c,-d";
Iterable<String> iterator1=Splitter.on(",").split(sequence);
System.out.println("根据分隔符进行分割:");
for(String str: iterator1)
System.out.println(str);
//去掉分割后空的字符串
Iterable<String> iterator2=Splitter.on(",").omitEmptyStrings().split(sequence);
System.out.println("去掉分割后空的字符串:");
for(String str: iterator2)
System.out.println(str);
//去掉分后后字符串中的空格
Iterable<String> iterator3=Splitter.on(",").omitEmptyStrings().trimResults().split(sequence);
System.out.println("去掉分后后字符串中的空格:");
for(String str: iterator3)
System.out.println(str);
Iterable<String> iterator5=Splitter.on(",").omitEmptyStrings().trimResults(CharMatcher.is('-')).split(sequence);
System.out.println("去掉分后后字符串中'-':");
for(String str: iterator5)
System.out.println(str);
//以固定长度进行分割
//Iterable<String> iterator4=Splitter.fixedLength(2).split(sequence);
Iterable<String> iterator4=Splitter.fixedLength(2).trimResults().split(sequence);
System.out.println("以固定长度进行分割:");
for(String str: iterator4)
System.out.println(str);
//Splitter将处理结果处理成map类型
Map<String,String> map=Splitter.on(";").omitEmptyStrings().withKeyValueSeparator(",").split("a,c;quzer,yuanrq; , ;hello,csdn");
System.out.println("Splitter将处理结果处理成map类型:");
for(Map.Entry<String, String> entry: map.entrySet())
System.out.println("key="+ entry.getKey()+";value="+entry.getValue());
}
public static void main(String[] args) {
useMethod();
}
}
|
来自CODE的代码片
Splitter.java
运行结果:
a
-b
c
-d
去掉分割后空的字符串:
a
-b
c
-d
去掉分后后字符串中的空格:
a
-b
c
-d
去掉分后后字符串中'-':
a
b
c
d
以固定长度进行分割:
a,
,-
b,
c
,-
d
Splitter将处理结果处理成map类型:
key=a;value=c
key=quzer;value=yuanrq
key= ;value=
key=hello;value=csdn
guava的String之Splitter相关推荐
- 著名ai换脸网站_AI如何从著名的死去艺术家那里删除新音乐
著名ai换脸网站 Love it or hate it, the new Star Wars trilogy made history. Forget about film critics - I'm ...
- 用树莓派搭建公网个人下载平台aria2-pro,推荐6个优质种子资源站
很早zhaoolee就想搭个人下载站,趁着今年国庆时间充裕,我把下载站搭建到了树莓派上,并对公网开放:在任何地点,我只需通过网页提交下载任务,家中的树莓派就会自动把我需要的资源,日夜不间断地下载到我的 ...
- Google Guava Splitter
String.split的特殊情况 String[] split = ",a,,b,".split(",");for (String s : split) {S ...
- Guava入门~Splitter
String.split()问题:中间保留,最后丢弃 String commaSeparatedString = "Foo,,Bar,,Baz,,,"; String[] word ...
- GUAVA常用方法总结整理 String list map转换
2019独角兽企业重金招聘Python工程师标准>>> /** * list转换为字符串 */ @Test public void joinTest(){ List<Strin ...
- guava - Splitter
在我们使用 java 的split 方法时,如果不是对该方法足够了解,可能会得到我们意想之外的结果.记录下踩过的坑: public void test(){String str = "a,b ...
- 三、guava splitter
splitter工具可以按照分隔符拆分字符串: 示例: package org.example.model;import com.google.common.base.Splitter; import ...
- guava 字符串操作 Splitter
withKeyValueSeparator 按照 逗号拆分字符串,按照井号拆分k-v String json = "A=1,B=3"; Map<String,String&g ...
- 为什么我不建议你用阿里巴巴Java规范,而使用 Google Guava 编程?
点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 来自 | 张丰哲 链接 | www.jianshu.com ...
最新文章
- MySQL性能测试工具sysbench的安装和使用
- 在 Windows 7 下安装 Hyper-V manager
- 数据库高可用架构(MySQL、Oracle、MongoDB、Redis)
- Oracle WebCenter 11g 快速开发指南--翻译(一)
- linux python代码编辑器,Linux上的Python编辑器
- date new 转换时区_Pandas 时间序列 时区控制
- 【自用】nginx.conf
- 知乎爬虫之4:抓取页面数据
- oracle 02380,oracle 11.2.0.3 fornb…
- c语言1000内亲密对数,《C语言程序的设计上机指导》项目五函数及其应用.pptx
- 基于Ameoba实现mysql读写分离
- 小朋友(洛谷-P3852)
- 小窍门-在EXECL表中加入下拉列表
- Nginx从入门到掌握【(第3节(共3节)】
- IIS经典模式与集成模式
- iis部署错误:HTTP 错误 500.21 - Internal Server Error
- 单片机控制直流电机正反转
- 学习笔记(1):2020软考数据库系统工程师-基础知识培训视频-计算机系统--体系结构概述...
- 啦啦外卖41.7亲测可用!插件齐全!含顾客APP,商家APP ,骑手APP!需要的拿去研究
- 利用原生node.js连接sql数据库