converts uppercase letters in a string to lowercase in Java, and lowercase letters to uppercase
Watch Tips:
This provides 2 of ideas, but the first one is correct, the second is wrong; the second can be understood, but it should be noted that if a string is defined as a string, then the string cannot be changed; If you need to change, you should use StringBuffer, This example can also be a good illustration of the difference between StringBuffer and string.
in the following code, Exchange () can get the correct conclusion, ExChange2 () does not get the correct conclusion, because the above is said: If a string is defined as a string type, then the string cannot be changed
Converts the upper case of a string to lowercase and lowercase to uppercase: Idea 1 public
static String ExChange (String str) {
StringBuffer sb = new StringBuffer (); C3/>if (str!=null) {for
(int i=0;i<str.length (); i++) {
char c = str.charat (i);
if (Character.isuppercase (c)) {
sb.append (Character.tolowercase (c));
} else if (Character.islowercase (c)) {
sb.append (Character.touppercase (c));
}
}
return sb.tostring ();
}
Converts the upper case of a string to lowercase and lowercase to uppercase: Idea 2 public
static string ExChange2 (String str) {for
(int i=0;i<str.length (); i+ +) {
//If it is a lowercase if
(str.substring (i, i+1). Equals (Str.substring (i, i+1). toLowerCase ())) {
str.substring ( I, i+1). toUpperCase ();
else{
str.substring (i, i+1). toLowerCase ();
}
return str;
}
"Warm tips."
There are many common methods in the Apache Commons-lang package, such as the copy of the entire directory in the IO operation, to determine if a character is compatible with the email format, and to judge whether a character is a digital type ... So don't write everything yourself. Try to see if there is already a ready-made method.
For example, the above problem,. You can write "the project needs to have commons-lang this package, you can go to http://commons.apache.org/proper/commons-lang/download_lang.cgi download"
String str = "ABcD";
SYSTEM.OUT.PRINTLN (Stringutils.swapcase (str));
I saw the source code: stringutils.swapcase (str) The implementation of this method is actually similar to the first kind. Here is the official Apache Swapcase this method
/** * <p>swaps The case for a String changing upper and title case to * lower case, and lower case to u
Pper case.</p> * * <ul> * <li>upper case character converts to Lower case</li> * <li>title Case character converts to Lower case</li> * <li>lower case character converts to U Pper case</li> * </ul> * * <p>for a word based algorithm, @link ORG.APACHE.COMMONS.L
Ang3.text.wordutils#swapcase (String)}. * A {@code null} input String returns {@code null}.</p> * * <pre> * stringutils.swapcase (NULL) = null * Stringutils.swapcase ("") = "" * Stringutils.swapcase ("The Dog has a B
One ") =" The DOG has A bone "* </pre> * <p>note:this method changed in Lang version 2.0.
* It no longer performs a word based algorithm. * If you have use ASCII, you'll notice no chaNge. * That functionality are available in org.apache.commons.lang3.text.wordutils.</p> * * @param str the Stri ng to swap case, May is null * @return The changed string, {@code null} if NULL string input/public Stati
C string Swapcase (String str) {if (Stringutils.isempty (str)) {return str;
} char[] buffer = Str.tochararray ();
for (int i = 0; i < buffer.length i++) {char ch = buffer[i];
if (character.isuppercase (ch)) {Buffer[i] = character.tolowercase (CH);
else if (character.istitlecase (ch)) {Buffer[i] = character.tolowercase (CH);
else if (character.islowercase (ch)) {Buffer[i] = character.touppercase (CH);
Return the new String (buffer); }