[Jakarta commons] use the stringutil class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.