Guava:Strings 字符串处理工具

简介

StringCharSequence 实例有关的静态实用程序方法。

类方法说明

官方文档:Strings (Guava: Google Core Libraries for Java 27.0.1-jre API)

方法名称 方法说明
commonPrefix(CharSequence a, CharSequence b) 返回共同的前缀字符串.
commonSuffix(CharSequence a, CharSequence b) 返回共同的后缀字符串.
emptyToNull(@Nullable String string) 返回字符串非空字符串,返回原字符串。否则,返回null.
isNullOrEmpty(@Nullable String string) 判断字符串是不是null或者空字符串.
lenientFormat(@Nullable String template, Object... args) 返回给定的模板字符串,每次出现的"%s"都替换为args中对应的参数值; 或者,如果占位符和参数计数不匹配,则返回该字符串形式.
nullToEmpty(@Nullable String string) 返回字符串非null,返回原字符串。否则返回空字符串.
padEnd(String string, int minLength, char padChar) 返回字符串,长度最少是minLength,长度不够的话用重复的padChar填充.
padStart(String string, int minLength, char padChar) 返回字符串,长度最少是minLength,长度不够的话用重复的padChar填充.
repeat(String string, int count) 返回string重复count次.

使用Demo

java 复制代码
import com.google.common.base.Strings;
import org.junit.Test;

/**
 * Strings 字符串处理
 */
public class StringsTest {

    @Test
    public void strings() {
        //1.判空
        System.out.println(Strings.isNullOrEmpty("")); // true
        System.out.println(Strings.isNullOrEmpty(null)); // true
        System.out.println(Strings.isNullOrEmpty("hello")); // false

        //2.将null转化为""
        System.out.println(Strings.nullToEmpty(null)); // ""

        //3.追加字符
        // 从尾部不断补充T只到总共8个字符,如果源字符串已经达到或操作,则原样返回。类似的有padStart
        System.out.println(Strings.padEnd("hello", 8, 'T')); // helloTTT

        //4.统计出现次数
        //返回string重复count次.
        System.out.println(Strings.repeat("hello", 3)); //hellohellohello

        //5.模板填充
        //返回给定的模板字符串,每次出现的"%s"都替换为args中对应的参数值; 或者,如果占位符和参数计数不匹配,则返回该字符串形式.
        String[] args = {"a", "b", "c"};
        System.out.println(Strings.lenientFormat("hi,%s,hello,%s,jack,%s", args));//hi,a,hello,b,jack,c

        String[] longArgs = {"a", "b", "c", "b"};
        System.out.println(Strings.lenientFormat("hi,%s,hello,%s,jack,%s", longArgs));//hi,a,hello,b,jack,c [b]

        String[] shortArgs = {"a", "b"};
        System.out.println(Strings.lenientFormat("hi,%s,hello,%s,jack,%s", shortArgs));//hi,a,hello,b,jack,%s

        //6.找寻相同前/后缀
        System.out.println(Strings.commonPrefix("Hello", "Het"));    // He
        System.out.println(Strings.commonPrefix("Hello", "Hit"));    // H
        System.out.println(Strings.commonPrefix("Hello", "hit"));    // 空
        System.out.println(Strings.commonPrefix("Hello", "Xit"));    // 空
        System.out.println(Strings.commonSuffix("world", "xid"));    // d
        System.out.println(Strings.commonSuffix("world", "xic"));    // 空
    }

}
相关推荐
夏天的味道٥3 小时前
使用 Java 执行 SQL 语句和存储过程
java·开发语言·sql
冰糖码奇朵5 小时前
大数据表高效导入导出解决方案,mysql数据库LOAD DATA命令和INTO OUTFILE命令详解
java·数据库·sql·mysql
好教员好5 小时前
【Spring】整合【SpringMVC】
java·spring
浪九天6 小时前
Java直通车系列13【Spring MVC】(Spring MVC常用注解)
java·后端·spring
堕落年代6 小时前
Maven匹配机制和仓库库设置
java·maven
功德+n6 小时前
Maven 使用指南:基础 + 进阶 + 高级用法
java·开发语言·maven
香精煎鱼香翅捞饭7 小时前
java通用自研接口限流组件
java·开发语言
ChinaRainbowSea7 小时前
Linux: Centos7 Cannot find a valid baseurl for repo: base/7/x86_64 解决方案
java·linux·运维·服务器·docker·架构
囧囧 O_o7 小时前
Java 实现 Oracle 的 MONTHS_BETWEEN 函数
java·oracle
去看日出7 小时前
RabbitMQ消息队列中间件安装部署教程(Windows)-2025最新版详细图文教程(附所需安装包)
java·windows·中间件·消息队列·rabbitmq