Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 887
James Avatar asked Nov 13 '09 15:11

James


People also ask

How do you reverse capitalization in Java?

Check each character, and use Character. isUpperCase / or Character. isLowerCase to test and reverse the capitalization.

Is there reverse method in string in Java?

String class in Java does not have reverse() method, however, the StringBuilder class has built-in reverse() method. StringBuilder class do not have toCharArray() method, while String class does have toCharArray() method.

How do you change a character case in Java?

toUpperCase(char ch) converts the character argument to uppercase using case mapping information from the UnicodeData file. Note that Character. isUpperCase(Character.


1 Answers

Apache Commons StringUtils has a swapCase method.

like image 116
Jacob Mattison Avatar answered Dec 01 '22 22:12

Jacob Mattison