org.apache.commons.lang.StringUtils

本文介绍了 Apache Commons Lang 库中的 StringUtils 类提供的多种实用字符串操作方法,包括检查空字符串、缩写字符串、查找嵌套字符串等。通过具体示例展示了如何使用这些方法简化字符串处理任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

缩写字符串:

  1.  String test  =   " This is a test of the abbreviation. "   
  2. System.out.println( StringUtils.abbreviate( test,  10  ) );  
  3.   
  4. [Console输出]  
  5. This is   

查找嵌套字符串:

  1.  String htmlContent  =   " <html>/n "   +   
  2.                       "   <head>/n "   +   
  3.                       "     <title>Test Page</title>/n "   +   
  4.                       "   </head>/n "   +   
  5.                       "   <body>/n "   +   
  6.                       "     <p>This is a TEST!</p>/n "   +   
  7.                       "   </body>/n "   +   
  8.                       " </html> " ;  
  9.   
  10.  //  Extract the title from this XHTML content    
  11.  String title  =  StringUtils.substringBetween(htmlContent,  " <title> " ,  " </title> " );  
  12. System.out.println(  " Title:  "   +  title );  
  13.   
  14. [Console输出]  
  15. Title: Test Page   

验证字符串:

  1.  String test1  =   " ORANGE " ;  
  2.   
  3. String test2  =   " ICE9 " ;  
  4.   
  5. String test3  =   " ICE CREAM " ;  
  6.   
  7. String test4  =   " 820B Judson Avenue " ;  
  8.   
  9. //判断字母  
  10.  boolean  t1val  =  StringUtils.isAlpha( test1 );  //  returns true   
  11.  //判断是不是字母数字  
  12.  boolean  t2val  =  StringUtils.isAlphanumeric( test2 );  //  returns true   
  13.  //字母空格  
  14.  boolean  t3val  =  StringUtils.isAlphaSpace( test3 );  //  returns true   
  15.  //字母数字空格  
  16.  boolean  t4val  =    
  17.   
  18.     StringUtils.isAlphanumericSpace( test4 );  //  returns true   

计算字符串出现频率:

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

  2.    Reader reader  =   new  FileReader( manuscriptFile );    

  3.    StringWriter stringWriter  =   new  StringWriter( );   

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

  5.   String manuscript  =  stringWriter.toString( );  

  6.   //  Convert string to lowercase   

  7.   manuscript  =  StringUtils.lowerCase(manuscript);  

  8.  //  count the occurrences of "futility"   

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

 

比较不同字符串:

  1.  int  dist  =  StringUtils.getLevenshteinDistance(  " Word " ,  " World "  );  
  2.   
  3. String diff  =  StringUtils.difference(  " Word " ,  " World "  );  
  4.   
  5.  int  index  =  StringUtils.indexOfDifference(  " Word " ,  " World "  );  
  6.   
  7. System.out.println(  " Edit Distance:  "   +  dist );  
  8.   
  9. System.out.println(  " Difference:  "   +  diff );  
  10.   
  11. System.out.println(  " Diff Index:  "   +  index );  
  12.   
  13. [Console输出]  
  14. Edit Distance:  2   
  15.    
  16. Difference: ld  
  17.   
  18. Diff Index:  3   


 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值