I. Preface
Java code farmers do not know Apache, so they will never miss it. There are many open-source projects under the company, all of which are sky-breaking. Today, let's talk about commons. Click this link to go to the Apache commons homepage, including logging, pool, net, ongl, El, Io, DBCP, email, collection, Lang ...... And so on. Lang, the main character of this article, is our most commonly used tool as a supplement to JDK. How can we not go into details!
: Http://commons.apache.org/proper/commons-lang/download_lang.cgi
Ii. string processing class (stringutils)
Org. Apache. commons. lang3.stringutils inherits objects, operations on String
That arenul
l
Safe. The so-called null safe means that there will be no nullpointerexception exception in string operations. Is it very practical! In the future, we will not be afraid of NULL pointer exceptions everywhere. First, let's take a look at the methods for this class in the official documentation:
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/49/9E/wKiom1QWyv7wXBz4AAU5PodFOcs211.jpg "Title =" qq 40915191747.jpg "alt =" wkiom1qwyv7wxbz4aau5podfocs211.jpg "/>
These methods basically look at the method name to guess its approximate effect.
// Shorten it to a certain length,... end. actually it is (substring (STR, 0, max-3) + "... ") // public static string abbreviate (string STR, int maxwidth) stringutils. abbreviate ("abcdefg", 6); // --- "ABC... "// whether the suffix ending with the string matches the suffix you want to end. If not, add the suffix stringutils. appendifmissing ("ABC", "XYZ"); // --- "abcxyz" stringutils. appendifmissingignorecase ("abcxyz", "XYZ"); // --- "abcxyz" // converts uppercase and lowercase letters to stringutils. capitalize ("cat"); // --- "cat" stringutils. uncapitalize ("C At "); // ---" cat "// string is expanded to the specified size and centered (if the expansion size is smaller than the original size, the original character is returned, if the expansion is negative, it is 0.) stringutils. center ("ABCD", 2); // --- "ABCD" stringutils. center ("AB",-1); // --- "AB" stringutils. center ("AB", 4); // --- "AB" stringutils. center ("A", 4, "YZ"); // --- "yayz" stringutils. center ("ABC", 7, ""); // --- "ABC" // remove "\ n", "\ r ", or "\ r \ n" stringutils. chomp ("ABC \ r \ n"); // --- "ABC" // determines whether a string contains another string, stringutils. contains ("ABC "," Z "); // --- false stringutils. containsignorecase ("ABC", "A"); // --- true // count the number of times a string appears in another string stringutils. countmatches ("Abba", "A"); // --- 2 // Delete the space stringutils from the string. deletewhitespace ("AB C"); // --- "ABC" // compares two strings and returns differences. Returns the stringutils string that is different from the first parameter in the second parameter. difference ("ABCDE", "abxyz"); // --- "XYZ" // check whether the end Suffix of the string matches stringutils. endswith ("abcdef", "def"); // --- true stringutils. endswithignorecase ("abcdef", "def"); // --- true stringutils. endswithany ("abcxyz", new string [] {null, "XYZ", "ABC"}); // --- true // check whether the start string matches stringutils. startswith ("abcdef", "ABC"); // --- true stringutils. startswithignorecase ("abcdef", "ABC"); // --- tr UE stringutils. startswithany ("abcxyz", new string [] {null, "XYZ", "ABC"}); // --- true // judge whether the two strings are the same stringutils. equals ("ABC", "ABC"); // --- true stringutils. equalsignorecase ("ABC", "ABC"); // --- true // compares the character sequences of all elements in the string array. If the start is consistent, a consistent string is returned, if no value exists, "" stringutils. getcommonprefix (New String [] {"ABCDE", "abxyz"}); // --- "AB" // the position where the forward lookup character first appears in the string stringutils. indexof ("aabaabaa", "B"); // --- 2 stringutils. indexof ("aabaab AA "," B ", 3); // --- 5 (search after badge 3) stringutils. ordinalindexof ("aabaabaa", "A", 3); // --- 1 (locate the nth occurrence position) // reverse searches for the first occurrence position of the string stringutils. lastindexof ("aabaabaa", 'B'); // --- 5 stringutils. lastindexof ("aabaabaa", 'B', 4); // --- 2 stringutils. lastordinalindexof ("aabaabaa", "AB", 2); // --- 1 // judge string uppercase and lowercase stringutils. isalluppercase ("ABC"); // --- true stringutils. isalllowercase ("ABC"); // --- false // determines whether it is null (Note: isblank and isempty Difference) stringutils. isblank (null); stringutils. isblank (""); stringutils. isblank (""); // --- true stringutils. isnoneblank ("", "bar"); // --- false stringutils. isempty (null); stringutils. isempty (""); // --- true stringutils. isempty (""); // --- false stringutils. isnoneempty ("", "bar"); // --- true // judge the string number stringutils. isnumeric ("123"); // --- false stringutils. isnumeric ("12 3"); // --- false (operator number, decimal point, space… not recognized ......) Stringutils. isnumericspace ("12 3"); // --- true // Add a separator to the array // stringutils. join ([1, 2, 3], ';'); // --- "1; 2; 3" // converts the case to stringutils. uppercase ("ABC"); // --- "ABC" stringutils. lowercase ("ABC"); // --- "ABC" stringutils. swapcase ("the dog has a bone"); // --- "the dog has a bone" // Replace the string content ...... (Replacepattern, replceonce) stringutils. replace ("ABA", "A", "Z"); // --- "ZBZ" stringutils. overlay ("abcdef", "ZZ", 2, 4); // --- "abzzef" (Specified Region) stringutils. replaceeach ("ABCDE", new string [] {"AB", "D"}, new string [] {"W", "T "}); // --- "wcte" (if multiple groups are specified, replace AB-> W, D-> T) // repeat the stringutils character. repeat ('E', 3); // --- "eee" // reverse the string stringutils. reverse ("Bat"); // --- "tab" // delete a character stringutils. remove ("queued", 'U'); // --- "qeed" // delimiter String stringutils. split (".. b. C ",'. '); // --- ["A", "B", "C"] stringutils. split ("AB: CD: EF", ":", 2); // --- ["AB", "CD: EF"] stringutils. splitbywholeseparator ("AB -! -CD -! -Ef ","-! -", 2); // --- [" AB "," CD -! -Ef "] stringutils. splitbywholeseparatorpreservealltokens ("AB: CD: EF", ":"); //-["AB", "", "cd ", "Ef"] // remove spaces at the beginning and end, similar to trim ...... (Stripstart, stripend, stripall, stripaccents) stringutils. strip ("AB C"); // --- "AB C" stringutils. striptonull (null); // --- null stringutils. striptoempty (null); // --- "" // truncate the stringutils string. substring ("ABCD", 2); // --- "cd" stringutils. substring ("abcdef", 2, 4); // --- "cd" // left and right are truncated from the left (right) to the stringutils. left ("ABC", 2); // --- "AB" stringutils. right ("ABC", 2); // --- "BC" // truncates M characters from the nth digit n m stringutils. mid ("abcdefg", 2, 4); // --- "cdef" stringutils. substringbefore ("abcba", "B"); // --- "A" stringutils. substringbeforelast ("abcba", "B"); // --- "ABC" stringutils. substringafter ("abcba", "B"); // --- "CBA" stringutils. substringafterlast ("abcba", "B"); // --- "A" stringutils. substringbetween ("tagabctag", "tag"); // --- "ABC" stringutils. substringbetween ("yabczyabcz", "Y", "Z"); // --- "ABC"
Iii. Introduction to other categories
Randomstringutils:
// Generate random N-digit randomstringutils. randomnumeric (n); // generate a random string named N in the specified string. random (n, "abcdefghijk"); // specify to generate a random string system from a character or number. out. println (randomstringutils. random (n, true, false); system. out. println (randomstringutils. random (n, false, true ));
Numberutils:
// Select the maximum value from the array.
Numberutils. Max (New int [] {1, 2, 3, 4}); // --- 4
// Judge whether all strings are integers
Numberutils. isdigits ("153.4"); // -- false
// Determine whether the string is a valid number.
Numberutils. isnumber ("0321.1"); // --- false
Arrayutils:
// Create an array string [] array = arrayutils. toarray ("1", "2"); // determines whether the two data are equal. if the content is the same and the order is the same, true arrayutils is returned. isequals (arr1, arr2); // determines whether the array contains an object arrayutils. contains (ARR, "33"); // converts a two-dimensional array to map = arrayutils. tomap (New String [] [] {"red", "# ff0000" },{ "green", "#00ff00" },{ "blue ", "# 0000ff "}});
Dateutils:
// Date plus N days dateutils. adddays (new date (), n); // determines whether dateutils is the same day. issameday (date1, date2); // converts string time to date dateutils. parsedate (STR, parsepatterns );
Iv. Conclusion
This article briefly introduces some common tool classes in commons-Lang, and does not list many useful tools. You still need to check the document and try it out before you can understand its simplicity.
This article is from the "Learn and think" blog, please be sure to keep this source http://linhongyu.blog.51cto.com/6373370/1553329
Introduction to Apache commons-Lang