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

后端 未结 9 1412
感情败类
感情败类 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 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.

提交回复
热议问题