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

James picture James · Nov 13, 2009 · Viewed 25k times · Source

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.

so "AbCdE123" becomes "aBcDe123"

I guess there must be a way to iterate through the String and flip each character, or perhaps some regular expression that could do it.

Answer

Jacob Mattison picture Jacob Mattison · Nov 13, 2009

Apache Commons StringUtils has a swapCase method.