本文主要是介绍StringUtil类,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
来源:http://blog.163.com/yuping_fir228/blog/static/7509253320086811300172/
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
这篇关于StringUtil类的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!