lang3之StringUtils

该对象提供了String字符串操作的一些常用方法。

目录

appendIfMissing(如果字符串没以某个字符串结尾为结尾,就添加该结尾)

abbreviate(字符串缩略)


字符串判断

 

方法名使用比较
isBlank
 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("bob")     = false
 StringUtils.isBlank("  bob  ") = false
没有字符就返回true,空格也返回空

isEmpty

 StringUtils.isEmpty(null)      = true
 StringUtils.isEmpty("")        = true
 StringUtils.isEmpty(" ")       = false
 StringUtils.isEmpty("bob")     = false
 StringUtils.isEmpty("  bob  ") = false
含有空格,不反悔空

 

与之对应的还有  (这里的”空“ 都泛指上面表格对应方法的true)

其他空判断
isNotBlank(CharSequence cs)
isNotEmpty(CharSequence cs)
非空判断
isAnyBlank(CharSequence... css)
isAnyEmpty(CharSequence... css)
是否存在空字符串(多个中有一个为空,返回true)
isNoneBlank(CharSequence... css)
isNoneEmpty(CharSequence... css)
是否全部都不是空(都不是空返回true)
isAllBlank(CharSequence... css)
isAllEmpty(CharSequence... css)
是否全为空(全是空,返回true)

appendIfMissing(如果字符串没以某个字符串结尾为结尾,就添加该结尾)

例如:String a = "abc"; 

我们规定 字符串必选以 yes 为结尾,如果abc 没有以 yes 结尾,就自动附加上,如果已经以yes 结尾那么不处理;例如:

 

官方提供了两个相关方法:

static StringappendIfMissing(String str, CharSequence suffix, CharSequence... suffixes)

Appends the suffix to the end of the string if the string does not already end with any of the suffixes.

static StringappendIfMissingIgnoreCase(String str, CharSequence suffix, CharSequence... suffixes)

Appends the suffix to the end of the string if the string does not already end, case insensitive, with any of the suffixes.

解释一下第三个参数,意思就是可以规定多个结尾的字符串,比喻,"yes","no","not" 等,只要其中一个符合便不附加,如果不符合,就把第一个附加到后面,也就是 “yes”

第二个方法是忽略大写匹配

 

abbreviate(字符串缩略)

该方法可以实现字符串的省略,设置字符长度,后面三个字符显示为 " ... " (如果字符串长度比设置的小,就不会显示了)。如:

官网提供了5个相关方法:

static Stringabbreviate(String str, int maxWidth)

Abbreviates a String using ellipses.

static Stringabbreviate(String str, int offset, int maxWidth)

Abbreviates a String using ellipses.

static Stringabbreviate(String str, String abbrevMarker, int maxWidth)

Abbreviates a String using another given String as replacement marker.

static Stringabbreviate(String str, String abbrevMarker, int offset, int maxWidth)

Abbreviates a String using a given replacement marker.

static StringabbreviateMiddle(String str, String middle, int length)

Abbreviates a String to the length passed, replacing the middle characters with the supplied replacement String.

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

keep-go-on

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值