org.apache.commons.lang.StringUtils

 

org.apache.commons.lang.StringUtils中提供许多有用的字符串操作方法,了解这些方法,我们可以避免许多不必要的重复工作。下面介绍其中比较有用的几个方法:

检查空字符串:

StringUtils.isBlank(String str);
StringUtils.isNotBlank(String str);

缩写字符串:

String test  =   " This is a test of the abbreviation. "
System.out.println( StringUtils.abbreviate( test, 
10  ) );

[Console输出]
This is

查找嵌套字符串:
String htmlContent  =   " <html>/n "   +
                     
"   <head>/n "   +
                     
"     <title>Test Page</title>/n "   +
                     
"   </head>/n "   +
                     
"   <body>/n "   +
                     
"     <p>This is a TEST!</p>/n "   +
                     
"   </body>/n "   +
                     
" </html> " ;

//  Extract the title from this XHTML content 

String title  =  StringUtils.substringBetween(htmlContent,  " <title> " " </title> " );
System.out.println( 
" Title:  "   +
 title );

[Console输出]
Title: Test Page

验证字符串:
String test1  =   " ORANGE " ;

String test2 
=   " ICE9 "
;

String test3 
=   " ICE CREAM "
;

String test4 
=   " 820B Judson Avenue "
;


boolean  t1val  =  StringUtils.isAlpha( test1 );  //  returns true


boolean  t2val  =  StringUtils.isAlphanumeric( test2 );  //  returns true

boolean  t3val  =  StringUtils.isAlphaSpace( test3 );  //  returns true

boolean  t4val  =  

    StringUtils.isAlphanumericSpace( test4 ); 
//  returns true

计算字符串出现频率:

File manuscriptFile  =   new  File( " manuscript.txt " );

Reader reader 
=   new
 FileReader( manuscriptFile );

StringWriter stringWriter 
=   new
 StringWriter( );

while ( reader.ready( ) )  { writer.write( reader.read( ) ); }


String manuscript 
=  stringWriter.toString( );

//  Convert string to lowercase


manuscript 
=  StringUtils.lowerCase(manuscript);

//  count the occurrences of "futility"


int  numFutility  =  StringUtils.countMatches( manuscript,  " futility "  );

比较不同字符串:
int  dist  =  StringUtils.getLevenshteinDistance(  " Word " " World "  );

String diff 
=  StringUtils.difference(  " Word " " World "
 );

int  index  =  StringUtils.indexOfDifference(  " Word " " World "
 );

System.out.println( 
" Edit Distance:  "   +
 dist );

System.out.println( 
" Difference:  "   +
 diff );

System.out.println( 
" Diff Index:  "   +
 index );

[Console输出]
Edit Distance: 
2


Difference: ld

Diff Index: 
3
### 区别 - **版本与兼容性**:`org.apache.commons.lang.StringUtils` 是 Apache Commons Lang 早期版本中的类;`org.apache.commons.lang3.StringUtils` 是 Apache Commons Lang 3.x 版本引入的类,3.x 版本在设计上进行了许多改进,修复了早期版本的一些问题,同时增强了功能和性能,并且更符合现代 Java 编程的最佳实践。 - **功能特性**:`org.apache.commons.lang3.StringUtils` 相较于旧版本有更多的功能和优化。例如,在处理空值和空字符串时更加安全和健壮,提供了更多实用的字符串处理方法。 ### 使用方法 以下是使用 `org.apache.commons.lang3.StringUtils` 的一些常见示例: ```java import org.apache.commons.lang3.StringUtils; public class StringUtilsExample { public static void main(String[] args) { // 判断字符串是否为空 boolean isEmpty = StringUtils.isEmpty(""); System.out.println("字符串是否为空: " + isEmpty); // 去除字符串两端的空白 String trimmed = StringUtils.trim(" hello "); System.out.println("去除两端空白后的字符串: " + trimmed); // 拼接字符串 String joined = StringUtils.join(new String[]{"a", "b", "c"}, ","); System.out.println("拼接后的字符串: " + joined); } } ``` 使用 `org.apache.commons.lang.StringUtils` 的方法与之类似,只是需要替换导入的类路径。 ### 特性 #### org.apache.commons.lang3.StringUtils - **安全性**:对空值和空字符串的处理更加安全,许多方法在传入 `null` 时不会抛出 `NullPointerException`,而是返回合理的结果,如 `isEmpty(null)` 返回 `true`。 - **功能丰富**:提供了大量实用的字符串处理方法,如字符串的拼接、分割、替换、大小写转换、去除空白等。 - **性能优化**:在实现上进行了性能优化,能够更高效地处理字符串操作。 #### org.apache.commons.lang.StringUtils - **基本功能**:具备一些基本的字符串处理功能,如判断字符串是否为空、去除空白等,但功能相对较少。 - **兼容性**:在一些旧项目中可能仍在使用,以保持与旧代码的兼容性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值