Uppercase and lowercase conversions in strings in Java

Source: Internet
Author: User

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.

  1. Convert uppercase in a string to lowercase and lowercase to uppercase: idea 1
  2. Public static string ExChange (string Str) {
  3. StringBuffer sb = new StringBuffer ();
  4. if (str!=null) {
  5. for (int i=0;i<str.length (); i++) {
  6. char c = Str.charat (i);
  7. if (character.isuppercase (c)) {
  8. Sb.append (character.tolowercase (c));
  9. }Else if (character.islowercase (c)) {
  10. Sb.append (character.touppercase (c)); One interview asked append () which class was Inside.
  11. }
  12. }
  13. }
  14. return sb.tostring ();
  15. }
  16. Convert uppercase in a string to lowercase and lowercase to uppercase: idea 2
  17. Public static string ExChange2 (string Str) {
  18. for (int i=0;i<str.length (); i++) {
  19. //if it is lowercase
  20. if (str.substring (i, i+1). equals (str.substring (i, i+1). tolowercase ())) {
  21. Str.substring (i, i+1). touppercase ();
  22. }else{
  23. Str.substring (i, i+1). tolowercase ();
  24. }
  25. }
  26. return str;
  27. }

 

"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.

 
    1. /**
    2. * <p>swaps The case of a String changing upper and title case to
    3. * Lower case, and lower case to upper case.</p>
    4. *
    5. * <ul>
    6. * <li>upper Case character converts to Lower case</li>
    7. * <li>title Case character converts to Lower case</li>
    8. * <li>lower Case character converts to Upper case</li>
    9. * </ul>
    10. *
    11. * <p>for A word based algorithm, see {@link org.apache.commons.lang3.text.wordutils#swapcase (String)}.
    12. * A {@code null} input String returns {@code null}.</p>
    13. *
    14. * <pre>
    15. * Stringutils.swapcase (null) = null
    16. * Stringutils.swapcase ("") = ""
    17. * Stringutils.swapcase ("the Dog has a BONE") = "the dog has a BONE"
    18. * </pre>
    19. *
    20. * <p>note:this method changed in Lang version 2.0.
    21. * It no longer performs a word based algorithm.
    22. * If you have only the use of ASCII, you'll notice no Change.
    23. * That functionality was available in org.apache.commons.lang3.text.wordutils.</p>
    24. *
    25. * @param str The String to swaps case, could be null
    26. * @return The changed string, {@code null} if null string input
    27. */
    28. Public static string Swapcase (string Str) {
    29. if (stringutils.isempty (str)) {
    30. return str;
    31. }
    32. char[] buffer = Str.tochararray ();
    33. for (int i = 0; i < buffer.length; i++) {
    34. char ch = buffer[i];
    35. if (character.isuppercase (ch)) {
    36. buffer[i] = Character.tolowercase (ch);
    37. } Else if (character.istitlecase (ch)) {
    38. buffer[i] = Character.tolowercase (ch);
    39. } Else if (character.islowercase (ch)) {
    40. buffer[i] = Character.touppercase (ch);
    41. }
    42. }
    43. return New String (buffer);
    44. }

Uppercase and lowercase conversions in strings in Java

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.