Tell me you need to translate all the big letters in the string into small ones and vice versa. Nothing but how to split the split into substrings \ under \ substrings and compare them with an array of letters does not occur.

    2 answers 2

    Eh, why are you with such a nickname and asking such simple questions :) There are lots of options!

    Option number 1

    class ReverseCase1 { public static void main(String[] args) { String original = "Hello, World!"; char[] chars = original.toCharArray(); for(int i = 0; i < chars.length; i++) { if(chars[i] > 64 && chars[i] < 91) { chars[i] += 32; } else if(chars[i] > 96 && chars[i] < 123) { chars[i] -= 32; } } System.out.println(chars); } } 

    Option number 2

     class ReverseCase2 { public static void main(String[] args) { String original = "Hello, World!"; char[] chars = original.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); } } System.out.println(chars); } } 

    Option number 3

     class ReverseCase3 { public static void main(String[] args) { String original = "Hello, World!"; String reversed = original.codePoints() .map(c -> Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c)) .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) .toString(); System.out.println(reversed); } } 
    • 3 option is 1.8 java? - pavel
    • @pavel yes, using the Stream API and lambdas, which appeared in the eight. - Sergey Gornostaev
    • Why not use the ternary operator in the first and second versions, as well as the third one, is it much more appropriate than a double if ? - pavlofff
    • @pavlofff is much more interesting to me, is it possible to somehow manage without the ternary operator in the third version. With my heart I feel that there is a more elegant way to control the flow, but I can not figure out which one. - Sergey Gornostaev
    • 2
      String reversed = original.codePoints () .map (c -> Character.isUpperCase (c)? Character.toLowerCase (c): Character.toUpperCase (c)) .mapToObj (i -> Character.toString ((char) i) ) .collect (Collectors.joining ()); - Russtam 5:52 pm

    The answer lies in the question or I misunderstood:

    toUpperCase ()

    toLowerCase ()

    UPDATE: Option if I still misunderstood

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

    • I think is wrong. - pavel