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.开发者_StackOverflow 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.
Apache Commons StringUtils has a swapCase method.
I don't believe there's anything built-in to do this (it's relatively unusual). This should do it though:
public static String reverseCase(String text)
{
char[] chars = text.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);
}
}
return new String(chars);
}
Note that this doesn't do the locale-specific changing that String.toUpperCase/String.toLowerCase does. It also doesn't handle non-BMP characters.
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.
Based on Faraz's approach, I think the character conversion can be as simple as:
t += Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c);
Java 8 and above:
String myString = "MySampleString123";
System.out.println(myString.chars().map(c -> Character.isLetter(c) ? c ^ ' ' : c).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString());
Code which is inverting the case of letter, is important to notice:
Character.isLetter(c) ? c ^ ' ' : c
I think the simplest solution to understand would be something like:
public static String reverseCase(String text) {
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray())
sb.append(Character.isUpperCase(c) ?
Character.toLowerCase(c) :
Character.toUpperCase(c));
return sb.toString();
}
I think you can read through this easier and StringBuilder has an append(char) method anyways + Character.toUpperCase
and toLowerCase
are both just static methods. I just felt bad that the only StringBuilder example had ascii index arithmetics included as well.
For those who don't like ternary expressions, here's the equivalent:
public static String reverseCase(String text) {
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray())
if (Character.isUpperCase(c))
c = Character.toLowerCase(c);
else
c = Character.toUpperCase(c);
sb.append(c);
return sb.toString();
}
EDIT Java 8+ (StringBuilder collector code taken from @Arun's answer)
public static String reverseCase(String text) {
return text.chars()
.map(c -> Character.isUpperCase(c) ?
Character.toLowerCase(c) :
Character.toUpperCase(c))
.collect(
StringBuilder::new,
StringBuilder::appendCodePoint,
StringBuilder::append)
.toString();
}
We can also use a StringBuilder object, as it has character replacing methods. However, it might take some extra space to store the StringBuilder object. So, it will help if space does not matter and keep the solution simple to understand.
String swapCase(String text) {
StringBuilder textSB = new StringBuilder(text);
for(int i = 0; i < text.length(); i++) {
if(text.charAt(i) > 64 && text.charAt(i) < 91)
textSB.setCharAt(i, (char)(text.charAt(i) + 32));
else if(text.charAt(i) > 96 && text.charAt(i) < 123)
textSB.setCharAt(i, (char)(text.charAt(i) - 32));
}
return textSB.toString();
}
public class ReverseCase {
public static void main(String[] args){
char[] char_arr = args[0].toCharArray();
for (int i = 0; i < char_arr.length; i++) {
if (Character.isLowerCase(char_arr[i])) {
char_arr[i] = Character.toUpperCase(char_arr[i]);
}else {
char_arr[i] = Character.toLowerCase(char_arr[i]);
}
}
String reversed = new String(char_arr);
System.out.println(reversed);
}
}
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);
}
}
精彩评论