问题
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.
回答1:
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.
回答2:
Apache Commons StringUtils has a swapCase method.
回答3:
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.
回答4:
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);
回答5:
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
回答6:
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();
}
回答7:
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);
}
}
回答8:
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);
}
}
来源:https://stackoverflow.com/questions/1729778/how-can-i-invert-the-case-of-a-string-in-java