We often operate on strings. Spring has implemented common processing functions. We can use the org. springframework. util. stringutils tool class to help us process strings.
Tool classes are organized as follows:
Stringutils. haslength (null) = false
Stringutils. haslength ("") = false
Stringutils. haslength ("") = true
Stringutils. haslength ("hello") = true
Stringutils. hastext (null) = false
Stringutils. hastext ("") = false
Stringutils. hastext ("") = false
Stringutils. hastext ("12345") = true
Stringutils. hastext ("12345") = true
// Whether it contains white space characters
Stringutils. containswhitespace (null) = false
Stringutils. containswhitespace ("") = false
Stringutils. containswhitespace ("A") = false
Stringutils. containswhitespace ("ABC") = false
Stringutils. containswhitespace ("ABC") = false
Stringutils. containswhitespace ("") = true
Stringutils. containswhitespace ("A") = true
Stringutils. containswhitespace ("ABC") = true
Stringutils. containswhitespace ("a B") = true
Stringutils. containswhitespace ("a B ")
Stringutils. trimwhitespace (null) = NULL;
Stringutils. trimwhitespace ("") = "";
Stringutils. trimwhitespace ("") = "";
Stringutils. trimwhitespace ("/t") = "";
Stringutils. trimwhitespace ("A") = "";
Stringutils. trimwhitespace ("A") = "";
Stringutils. trimwhitespace ("A") = "";
Stringutils. trimwhitespace ("a B") = "a B ";
Stringutils. trimleadingwhitespace (null) = NULL;
Stringutils. trimleadingwhitespace ("") = "";
Stringutils. trimleadingwhitespace ("") = "";
Stringutils. trimleadingwhitespace ("/t") = "";
Stringutils. trimleadingwhitespace ("A") = "";
Stringutils. trimleadingwhitespace ("A") = "";
Stringutils. trimleadingwhitespace ("A") = "";
Stringutils. trimleadingwhitespace ("a B") = "a B"
Stringutils. trimleadingwhitespace ("a B c") = "a B C"
Stringutils. trimtrailingwhitespace (null) = NULL;
Stringutils. trimtrailingwhitespace ("") = "";
Stringutils. trimtrailingwhitespace ("/t") = "";
Stringutils. trimtrailingwhitespace ("A") = "";
Stringutils. trimtrailingwhitespace ("A") = "";
Stringutils. trimtrailingwhitespace ("A") = "";
Stringutils. trimtrailingwhitespace ("a B") = "a B ";
Stringutils. trimtrailingwhitespace ("a B c") = "a B C ";
Stringutils. trimallwhitespace ("") = "";
Stringutils. trimallwhitespace ("") = "";
Stringutils. trimallwhitespace ("/t") = "";
Stringutils. trimallwhitespace ("A") = "";
Stringutils. trimallwhitespace ("A") = "";
Stringutils. trimallwhitespace ("A") = "";
Stringutils. trimallwhitespace ("a B") = "AB ";
Stringutils. trimallwhitespace ("a B C" = "ABC ";
// Count the number of times a substring appears in a string
Stringutils. countoccurrencesof (null, null) = 0;
Stringutils. countoccurrencesof ("S", null) = 0;
Stringutils. countoccurrencesof (null, "S") = 0;
Stringutils. countoccurrencesof ("erowoiueoiur", "werwer") = 0;
Stringutils. countoccurrencesof ("erowoiueoiur", "x") = 0;
Stringutils. countoccurrencesof ("erowoiueoiur", "") = 0;
Stringutils. countoccurrencesof ("erowoiueoiur", "") = 0;
Stringutils. countoccurrencesof ("erowoiueoiur", "E") = 2;
Stringutils. countoccurrencesof ("erowoiueoiur", "OI") = 2;
Stringutils. countoccurrencesof ("erowoiueoiur", "oiu") = 2;
Stringutils. countoccurrencesof ("erowoiueoiur", "oiur") = 1;
Stringutils. countoccurrencesof ("erowoiueoiur", "R") = 2;
// String replacement
String instring = "a6aazaaa77abaa ";
String oldpattern = "AA ";
String newpattern = "foo ";
// Simple replace
String S = stringutils. Replace (instring, oldpattern, newpattern );
S. Equals ("a6aazafoo77abfoo") = true;
// Non match: No change
S = stringutils. Replace (instring, "qwoeiruqopwieurpoqwieur", newpattern );
S. Equals (instring) = true
S = stringutils. Replace (instring, oldpattern, null );
S. Equals (instring) = true
// Null old pattern: shocould ignore
S = stringutils. Replace (instring, null, newpattern );
S. Equals (instring) = true
// Delete a string
String instring = "The quick brown fox jumped over the lazy dog ";
String nothe = stringutils. Delete (instring, "");
Nothe. Equals ("The quick brown fox jumped over lazy dog") = true;
String nohe = stringutils. Delete (instring, "he ");
Nohe. Equals ("t quick brown fox jumped over T lazy dog") = true;
String nosp = stringutils. Delete (instring ,"");
Nosp. Equals ("thequickbrownfoxjumpedoverthelazydog") = true;
String killend = stringutils. Delete (instring, "dog ");
Killend. Equals ("The quick brown fox jumped over the Lazy") = true;
String mismatch = stringutils. Delete (instring, "dxxcxcxog ");
Mismatch. Equals (instring) = true;
// Delete any character
// The source code is as follows:
// Char c = instring. charat (I );
// If the C value does not exist,-1 is returned.
// If (charstodelete. indexof (c) =-1 ){
// Out. append (C );
//}
String instring = "Able was I ere I saw Elba ";
String res = stringutils. deleteany (instring, "I ");
Res. Equals ("Able was ere saw Elba") = true;
Res = stringutils. deleteany (instring, "aeeba! ");
Res. Equals ("l ws I r I sw l") = true;
String mismatch = stringutils. deleteany (instring, "##$ # $ ^ ");
Mismatch. Equals (instring) = true;
// The source code is as follows: Return (STR! = NULL? "'" + STR + "'": NULL );
Assertequals ("'mystring'", stringutils. Quote ("mystring "));
Assertequals ("'' ", stringutils. Quote (""));
Assertnull (stringutils. Quote (null ));
// Change the first character to uppercase
Stringutils. capitalize (STR)
// Change the first character to lowercase.
Stringutils. uncapitalize (STR)
// Mypath/myfile.txt "->" myfile.txt
// Obtain the string file name and extension.
Stringutils. getfilename ("myfile"). Equals ("myfile") = true;
Stringutils. getfilename ("mypath/myfile". Equals ("myfile") = true;
Stringutils. getfilename ("mypath/myfile". Equals ("myfile") = true;
Stringutils. getfilename ("myfile.txt"). Equals ("myfile.txt") = true;
Stringutils. getfilename ("mypath/myfile.txt"). Equals ("myfile.txt") = true;
// Obtain the string extension, separated.
Stringutils. getfilenameextension ("myfile") = NULL;
Stringutils. getfilenameextension ("mypath/myfile") = NULL;
Stringutils. getfilenameextension ("myfile."). Equals ("") = true;
Stringutils. getfilenameextension ("mypath/myfile."). Equals ("") = true;
Stringutils. stringutils. getfilenameextension ("myfile.txt"). Equals ("TXT") = true;
Stringutils. getfilenameextension ("mypath/myfile.txt"). Equals ("TXT") = true;
// Remove the file name extension
Stringutils. stripfilenameextension (null) = true;
Stringutils. stripfilenameextension (""). Equals ("") = true;
Stringutils. stripfilenameextension ("myfile"). Equals ("myfile") = true;
Stringutils. stripfilenameextension ("mypath/myfile"). Equals ("mypath/myfile") = true;
Stringutils. stripfilenameextension ("myfile."). Equals ("myfile") = true;
Stringutils. stripfilenameextension ("mypath/myfile."). Equals ("mypath/myfile") = true;
Stringutils. stripfilenameextension ("mypath/myfile."). Equals ("mypath/myfile") = true;
Stringutils. stripfilenameextension ("myfile.txt"). Equals ("myfile") = true;
Stringutils. stripfilenameextension ("mypath/myfile.txt"). Equals ("mypath/myfile") = true;