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" +
"<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 output]
Title: test page
Verification string:
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
Calculate the occurrence frequency of a string:
File manuscriptfile = new file ("manuscript.txt ");
Reader reader = new filereader (manuscriptfile );
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: LD
Diff index: 3