星空网 > 软件开发 > Java

org.apache.commons.lang.StringUtils中常用的方法

org.apache.commons.lang.StringUtils中常用的方法,这里主要列举String中没有,且比较有用的方法:

 

1. 检查字符串是否为空:

 

 static boolean isBlank(CharSequence str)  判断字符串是否为空或null;
 static boolean isNotBlank(CharSequence str) 判断字符串是否非空或非null;

 

 StringUtils.isBlank("a");
 返回结果为: false;

 

2. 缩进字符串:

 

 static String abbreviate(String str, int maxWidth) 缩进字符串,第二个参数至少为4(包括...)

 

 StringUtils.abbreviate("abcdefg", 20);
 返回结果为:abcdefg (正常显示)

 StringUtils.abbreviate("abcdefg", 4);
 返回结果为:a...

 

3. 首字母大写:

 

 static String capitalize(String str) 首字母大写
 static String uncapitalize(String str)首字母小写  

 

 StringUtils.capitalize("abcdefg");
 返回结果:Abcdefg

 

4. 字符串显示在一个大字符串的位置:

 

 static String center(String str, int size);  默认以空格填充
 static String center(String str, int size, String padString); 其余位置字符串填充
 public static String leftPad(String str,int size); 左侧空格填充
 public static String leftPad(String str,int size,String padStr);左侧字符串填充
 public static String rightPad(String str,int size); 左侧空格填充
 public static String rightPad(String str,int size,String padStr);左侧字符串填充
 

 StringUtils.center("abcdefg", 20);
 返回结果:      abcdefg      

 StringUtils.center("abcdefg", 20,"*_");
 返回结果:*_*_*_abcdefg*_*_*_*

 StringUtils.leftPad("abc", 10, "*");
 返回结果:*******abc

 

5. 重复字符串次数

 

 static String repeat(String str, int repeat);

 

 StringUtils.repeat("abc", 5); 
 返回结果:abcabcabcabcabc

 

6. 是否全是大写,是否全是小写(3.0版本)

 

 public static boolean isAllLowerCase(String str);
 public static boolean isAllUpperCase(String str);

 

 StringUtils.isAllLowerCase("abC");
 返回结果:false

 

7. 是否都是由字母组成:

 

 public static boolean isAlpha(String str);  只由字母组成
 public static boolean isAlphaSpace(String str); 只有字母和空格组成
 public static boolean isAlphanumeric(String str);只由字母和数字组成
 public static boolean isAlphanumericSpace(String str);只由字母数字和空格组成
 public static boolean isNumeric(String str);只由数字组成
 public static boolean isNumericSpace(String str);只由数字和空格组成

 

 StringUtils.isAlpha("a2bdefg");
 返回结果:false

 

8. 小字符串在大字符串中的匹配次数

 

public static int countMatches(String str,String sub);

 

StringUtils.countMatches("ababsssababa", "ab");
 返回结果:4

 

9. 字符串倒转

 

 public static String reverse(String str);

 

 StringUtils.reverse("abcdef");
 返回结果:fedcba

 

10. 大小写转换,空格不动
 

 public static String swapCase(String str);

 

 StringUtils.swapCase("I am a-A*a")
 返回结果:i AM A-a*A




原标题:org.apache.commons.lang.StringUtils中常用的方法

关键词:Apache

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

紧急!大批卖家受影响,未合规或将被下架停售!:https://www.ikjzd.com/articles/1628591884688154625
爆款IP接连发起维权,跨境卖家们注意了!:https://www.ikjzd.com/articles/1628593769394462721
实例分析监测那些“黑过你”的竞争对手:https://www.ikjzd.com/articles/16286
一件代发大卖供应链揭秘!线上线下货源全盘点!:https://www.ikjzd.com/articles/1628602092059615234
eBay发布2022年第四季度财报,业绩超预期:https://www.ikjzd.com/articles/1628612485855305729
重磅!又一国正式加入RCEP 对华贸易额近6000亿!:https://www.ikjzd.com/articles/1628630282689830913
独家丨B站广告位可跳转美团APP B站为电商平台引流再升级 :https://www.kjdsnews.com/a/1836410.html
百崖大峡谷生态旅游景区(探秘中国西南自然风光):https://www.vstour.cn/a/363176.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流