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

后端 未结 9 1396
感情败类
感情败类 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:16

    I do realize that the given thread is very old, but there is a better way of solving it:

    class Toggle
    { 
        public static void main()
        { 
            String str = "This is a String";
            String t = "";
            for (int x = 0; x < str.length(); x++)
            {  
                char c = str.charAt(x);
                boolean check = Character.isUpperCase(c);
                if (check == true)
                    t = t + Character.toLowerCase(c);
                else
                    t = t + Character.toUpperCase(c);
            }
            System.out.println (t);
        }
    }
    

提交回复
热议问题