How can I invert the case of a String in Java?

后端 未结 9 1336
感情败类
感情败类 2020-11-30 12:16

I want to change a String so that all the uppercase characters become lowercase, and all the lower case characters become uppercase. Number characters are just ignored.

相关标签:
9条回答
  • 2020-11-30 12:56

    Apache Commons StringUtils has a swapCase method.

    0 讨论(0)
  • 2020-11-30 12:56
    public class ReverseCase {
        public  static void main(String[] args){ 
            char[] char_arr = args[0].toCharArray();
            for (int i = 0; i < char_arr.length; i++) {
                if (Character.isLowerCase(char_arr[i])) {
                    char_arr[i] = Character.toUpperCase(char_arr[i]);
                }else {
                    char_arr[i] = Character.toLowerCase(char_arr[i]);
                }
            }
            String reversed = new String(char_arr);
            System.out.println(reversed);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 12:56

    I think the simplest solution to understand would be something like:

    public static String reverseCase(String text) {
        StringBuilder sb = new StringBuilder();
        for (char c : text.toCharArray())
            sb.append((Character.isUpperCase(c)) ? 
                    Character.toLowerCase(c) : 
                    (Character.isLowerCase(c) ? Character.isUpperCase(c) : c));
        return sb.toString();
    }
    

    I think you can read through this easier and StringBuilder has an append(char) method anyways + Character.toUpperCase and toLowerCase are both just static methods. I just felt bad that the only StringBuilder example had ascii index arithmetics included as well.

    For those who don't like ternary expressions, here's the equivalent:

    public static String reverseCase(String text) {
        StringBuilder sb = new StringBuilder();
        for (char c : text.toCharArray())
            if (Character.isUpperCase(c)) 
                c = Character.toLowerCase(c);
            else if (Character.isLowerCase(c))
                c = Character.toUpperCase(c);
            sb.append(c);
        return sb.toString();
    }
    
    0 讨论(0)
  • 2020-11-30 13:01

    Java 8 and above:

    String myString = "MySampleString123";
    System.out.println(myString.chars().map(c -> Character.isLetter(c) ? c ^ ' ' : c).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString());
    

    Code which is inverting the case of letter, is important to notice:

    Character.isLetter(c) ? c ^ ' ' : c
    
    0 讨论(0)
  • 2020-11-30 13:04

    I don't believe there's anything built-in to do this (it's relatively unusual). This should do it though:

    public static String reverseCase(String text)
    {
        char[] chars = text.toCharArray();
        for (int i = 0; i < chars.length; i++)
        {
            char c = chars[i];
            if (Character.isUpperCase(c))
            {
                chars[i] = Character.toLowerCase(c);
            }
            else if (Character.isLowerCase(c))
            {
                chars[i] = Character.toUpperCase(c);
            }
        }
        return new String(chars);
    }
    

    Note that this doesn't do the locale-specific changing that String.toUpperCase/String.toLowerCase does. It also doesn't handle non-BMP characters.

    0 讨论(0)
  • 2020-11-30 13:15

    I guess there must be a way to iterate through the String and flip each character

    Correct. The java.lang.Character class provides you under each the isUpperCase() method for that. Test on it and make use of the toLowerCase() or toUpperCase() methods depending on the outcome. Append the outcome of each to a StringBuilder and you should be fine.

    0 讨论(0)
提交回复
热议问题