十个最常用的StringUtils方法

更新时间:2023-05-19 07:16:09 阅读: 评论:0

1.取得字符串的缩写 使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offt,width) 
函数介绍:在给定的width内取得testString的缩写,当testString的长度小于width则返回原字符串. 
例程:     
String test = "This is a test of the abbreviation.";     
String test2 = "Test";
     
System.out.println( StringUtils.abbreviate( test, 15 ) );     
System.out.println( StringUtils.abbreviate( test, 5,15 ) );     
System.out.println( StringUtils.abbreviate( test2, 10 ) ); 
《天鹅湖》输出如下: This is ...is Tes华为手机时间设置
2.查找嵌套字符串 使用函数:StringUtils.substringBetween(testString,header,tail) 
函数介绍:在testString中取得header和tail之间的字符串。不存在则返回空 
例程:     
String htmlContent = "ABC1234ABC4567";     
System.out.println(StringUtils.substringBetween(htmlContent, "1234", "4567"));    System.out.println(StringUtils.substringBetween(htmlContent, "12345", "4567")); 
输出如下:
ABC     
null 
3.重复字符串 使用函数:peat(repeatString,count) 
函数介绍:得到将repeatString重复count次后的字符串 
例程:     
System.out.println( peat( "*", 10));     
南山之寿System.out.println( peat( "China ", 5)); 
输出如下:     
**********     
China China China China China 
其他函数:( testString, count,repeatString ); 
函数介绍:把testString插入将repeatString重复多次后的字符串中间,得到字符串 的总长为count 
例程:     
System.out.println( ( "China", 11,"*")); 
输出如下:     
***China***
4.颠倒字符串 使用函数:ver(testString) 
函数介绍:得到testString中字符颠倒后的字符串 
例程:     
System.out.println( ver("ABCDE")); 
输出如下:     
EDCBA
5.空字符串检查 使用函数: StringUtils.isBlank(testString) 民开头的成语
函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回Fal 
例程:     
String test = "";
String test2 = "\n\n\t";     
String test3 = null;     
String test4 = "Test";
String test5 = "  "; 
System.out.println( "test blank? " + StringUtils.isBlank( test ) );     
System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );     
System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );     
System.out.println( "test4 blank? " + StringUtils.isBlank( test4 ) );
System.out.println( "test5 blank? " + StringUtils.isBlank( test5 ) ); 
输出如下: 
test blank? true 
test2 blank? true 
test3 blank? true 
test4 blank? Fal 
test5 blank? true
贫困户走访记录函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反. 
函数StringUtils.isEmpty(str). 判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0 (当str有空格时,结果为fal)
6.判断字符串内容的类型 
函数介绍: 
StringUtils.isNumeric( testString ) :如果testString全由数字组成返回True 
StringUtils.isAlpha( testString ) :如果testString全由字母组成返回True 
StringUtils.isAlphanumeric( testString ) :如果testString全由字母或数字组 成返回True 
StringUtils.isAlphaSpace( testString )  :如果testString全由字母或空格组 成返回True 
例程:     
烤鳗鱼的做法String state = "Virginia";     
System.out.println( "Is state number? " + StringUtils.isNumeric(state ) );     
System.out.println( "Is state alpha? " + StringUtils.isAlpha( state ));     
System.out.println( "Is state alphanumeric? " +StringUtils.isAlphanumeric( state ) );     
System.out.println( "Is state alphaspace? " + StringUtils.isAlphaSpace( state ) ); 
输出如下:     
Is state number? fal     
Is state alpha? true     
Is state alphanumeric? true     
事故原因分析Is state alphaspace? true
7.取得某字符串在另一字符串中出现的次数 使用函数:untMatches(testString,qString) 
函数介绍:取得qString在testString中出现的次数,未发现则返回零 
例程:     
System.out.untMatches( "Chine People", "e")); 
输出:     
4
8.字符串两端不能以指定的字符串中的字符开始或结尾, StringUtils.strip(String str, String stripChars)
函数介绍: 
去掉str两端的在stripChars中的字符。
如果str为null或等于"",则返回它本身;
如果stripChars为null或"",则返回strip(String str)。
例程: 
System.out.println(StringUtils.strip("Hello Word", "rdH"));
输出:
ello Wo
函数 StringUtils.stripStart(String str, String stripChars) 和 StringUtils.strip(String str, String stripChars) 相似,去掉str前端的在stripChars中的字符。
函数 StringUtils.stripEnd(String str, String stripChars) 和 StringUtils.strip(String str, String stripChars) 相似,去掉str末端的在stripChars中的字符。
9.删除或者替换字符串中的某个字符 使用函数:place(String text, String archString, String replacement);
函数介绍:将 text中出现的archString替换成replacement
怎么锁电脑屏幕例程:     
System.out.place("Hello Word e", "or", ""));
System.out.place("Hello Word e", " ", ""));
输出:     

本文发布于:2023-05-19 07:16:09,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/916084.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:字符串   函数   返回   介绍   字符   全由
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图