Org. Apache. commons. Lang. stringutils provides many useful string operation methods. By understanding these methods, we can avoid unnecessary repetitive work. The following describes several useful methods:
Check the Null String:
StringUtils.isBlank(String str);StringUtils.isNotBlank(String str);
Abbreviation string:
String test = "this is a test of the abbreviation." system. Out. println (stringutils. abbreviate (test, 10); [console output] This is
Search for nested strings:
String htmlcontent = "<HTML>/N" + "
Verification string:
String test1 = "orange"; string Test2 = "ice9"; string test3 = "ice cream"; string test4 = "820b Judson Avenue"; // judge the letter Boolean t1val = stringutils. isalpha (test1); // returns true // determines if it is a letter or number Boolean t2val = stringutils. isalphanumeric (Test2); // returns true // letter space Boolean t3val = stringutils. isalphaspace (test3); // returns true // alphanumeric Boolean t4val = stringutils. isalphanumericspace (test4); // returns true
Calculate the occurrence frequency of a string:
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 " );
Compare different strings:
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 output] edit distance: 2 difference: lddiff index: 3