How can I invert the case of a String in Java?
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.
Reverse a string in Java
You can use this:
new StringBuilder(hi).reverse().toString()
StringBuilder
was added in Java 5. For versions prior to Java 5, the StringBuffer
class can be used instead — it has the same API.
How to invert the case of a single character?
Use Character.isUpperCase()
to determine what case the character is, and Character.toLowerCase()
and Character.toUpperCase()
to change the case.
char c = 'A';
char result = Character.isUpperCase(c) ? Character.toLowerCase(c) : Character.toUpperCase(c);
System.out.println(result);
Which prints:
a
Java-Program to reverse upper case and lower case
The issue is that 32
is an integer, and let
is a char. Java will implicity convert the let
value to an int when it encounters let-32
and the result is the int value (for 'a') 96 or whatever.
You need to cast the result back to char:
(char)(let+32)
Convert java char inside string to lowerCase/upperCase
You are adding characters to the original string. Also, this means that your for
loop will never get to the end of the iteration of the for
loop, because originalString.length()
changes each loop also. It's an infinite loop.
Instead, create a StringBuilder
that stores the converted characters as you're iterating over the original string. The convert it to a String
and return it at the end.
StringBuilder buf = new StringBuilder(originalString.length());
for (int i = 0; i < originalString.length(); i++) {
char c = originalString.charAt(i);
if (Character.isUpperCase(c)) {
buf.append(Character.toLowerCase(c));
}
else if (Character.isLowerCase(c)) {
buf.append(Character.toUpperCase(c));
}
// Account for case: neither upper nor lower
else {
buf.append(c);
}
}
return buf.toString();
Inverse string case
You have just about everything correct, except for inside of your if
statement. You're setting n
equal to its toLowerCase
or toUpperCase
method, not its return value. You need to call those methods and set n
equal to their return values:
var convertString = function (str) {
var s = '';
var i = 0;
while (i < str.length) {
var n = str.charAt(i);
if (n == n.toUpperCase()) {
// *Call* toLowerCase
n = n.toLowerCase();
} else {
// *Call* toUpperCase
n = n.toUpperCase();
}
i += 1;
s += n;
}
return s;
};
convertString("lower UPPER");
The output that you're getting ('function toUpperCase() { [native code] }...
) is the result of each method being converted to a string, then concatenated to your result string. You can achieve the same result by running either of these commands in your console:
console.log("".toUpperCase);
console.log("".toUpperCase.toString());
The results of both are function toUpperCase() { [native code] }
.
Happy coding!
Related Topics
What Code Does the Compiler Generate for Autoboxing
Difference Between "This" And"Super" Keywords in Java
Is There Something Like Instanceof(Class<> C) in Java
Why Do I Need a Functional Interface to Work with Lambdas
Stream Filter of 1 List Based on Another List
Apache Spark - Foreach VS Foreachpartition When to Use What
Is Executorservice (Specifically Threadpoolexecutor) Thread Safe
How to Enable Wire Logging for a Java Httpurlconnection Traffic
Process Thymeleaf Variable as HTML Code and Not Text
Efficiently Finding the Intersection of a Variable Number of Sets of Strings
Add Jlabel with Image to Jlist to Show All the Images