Converts uppercase letters in a string to lowercase and lowercase to uppercase in Java
Watch Tips:
Here are 2 ideas, but the first one is right, the second is wrong, the second one is to understand, but it is important to note that if a string is defined as string, then this string cannot be changed, and if you need to change it, 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, and ExChange2 () cannot get the correct conclusion, because it says: if a string is defined as string, it cannot be changed.
- Convert uppercase in a string to lowercase and lowercase to uppercase: idea 1
- Public static string ExChange (string Str) {
- StringBuffer sb = new StringBuffer ();
- 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)); One interview asked append () which class was Inside.
- }
- }
- }
- return sb.tostring ();
- }
- Convert uppercase in 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 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".
Apache's Commons-lang Package has many of the methods we use most Often. for example, in the IO operation of the copy of the entire directory, to determine whether a character is in accordance with the format of email, to determine whether a character is a digital type ....
So don't write everything yourself. try to see if there is already a ready-made method in it.
such as the above question,. you can write this "project requires 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 read the source: stringutils.swapcase (str) The implementation of this method is actually the same as the first kind! The following is the official Apache Swapcase Method.
- /**
- * <p>swaps The case of a String changing upper and title case to
- * Lower case, and lower case to upper 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 Upper case</li>
- * </ul>
- *
- * <p>for A word based algorithm, see {@link org.apache.commons.lang3.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 BONE") = "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 only the use of ASCII, you'll notice no Change.
- * That functionality was available in org.apache.commons.lang3.text.wordutils.</p>
- *
- * @param str The String to swaps case, could be null
- * @return The changed string, {@code null} if null string input
- */
- Public static 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 New String (buffer);
- }
Uppercase and lowercase conversions in strings in Java