十个最常用的StringUtils方法

1.

取得字符串的缩写

 

使用函数

: StringUtils.abbreviate(testString,width)

StringUtils.abbreviate(testString,offset

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 a te... ...is a test... 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"));

1.

取得字符串的缩写

 

使用函数

: StringUtils.abbreviate(testString,width)

StringUtils.abbreviate(testString,offset

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 a te... ...is a test... 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"));

1.

取得字符串的缩写

 

使用函数

: StringUtils.abbreviate(testString,width)

StringUtils.abbreviate(testString,offset

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 a te... ...is a test... 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"));

1.取得字符串的缩写 使用函数: StringUtils.abbreviate(testString,width)和StringUtils.abbreviate(testString,offset,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 a te... ...is a test... 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"));

4.颠倒字符串 使用函数:StringUtils.reverse(testString)  函数介绍:得到testString中字符颠倒后的字符串  例程:      
System.out.println( StringUtils.reverse("ABCDE"));  输出如下:      EDCBA   
5.空字符串检查 使用函数: StringUtils.isBlank(testString)  
函数介绍: 当testString为空,长度为零或者仅由空白字符(whitespace)组成时,返回True;否则返回False  例程:      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? False  test5 blank? true 
函数StringUtils.isNotBlank(testString)的功能与StringUtils.isBlank(testString)相反.  函数StringUtils.isEmpty(str). 判断某字符串是否为空,为空的标准是str == null 或 str.length() == 0 (当str有空格时,结果为false)   
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? false      Is state alpha? true      Is state alphanumeric? true      Is state alphaspace? true   
7.取得某字符串在另一字符串中出现的次数 使用函数:StringUtils.countMatches(testString,seqString)  
函数介绍:取得seqString在testString中出现的次数,未发现则返回零  例程:      
System.out.println(StringUtils.countMatches( "Chinese 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.删除或者替换字符串中的某个字符 使用函数:StringUtils.replace(String text, String searchString, String replacement); 
函数介绍:将 text中出现的searchString替换成replacement 例程:      
System.out.println(StringUtils.replace("Hello Word e", "or", "")); System.out.println(StringUtils.replace("Hello Word e", " ", "")); 输出:      Hello Wd e HelloWorde 
  
10.删除或者替换字符串中的某个字符 使用函数:StringUtils.containsOnly((String str, char[] valid); 
函数介绍:判断是否字符串str仅包含字符数组valid中的字符,即字符串中的字符是否都在字符数组中。如果str为null,则返回false;如果str为"",则返回true 例程:      
StringUtils.containsOnly(null, *))                = false StringUtils.containsOnly("", *))                    = true StringUtils.containsOnly("afaf", ['a','f',' '])) = true StringUtils.containsOnly("af a", ['a','f',' '])) = true StringUtils.containsOnly("a", ['a','f',' ']))     = true StringUtils.containsOnly("afg", ['a','f',' ']))  = false
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值