I was refactoring some of my old code and then I found out that I'd used Character.toTitleCase()
method at some point and couldn't help myself wondering if Character.toUpperCase()
would be better.
I read their descriptions and didn't see any basic difference:
toUpperCase
Converts the character argument to uppercase using case mapping information from the UnicodeData file. Note that Character.isUpperCase(Character.toUpperCase(ch)) does not always return true for some ranges of characters, particularly those that are symbols or ideographs.
In general, String.toUpperCase() should be used to map characters to uppercase. String case mapping methods have several benefits over Character case mapping methods. String case mapping methods can perform locale-sensitive mappings, context-sensitive mappings, and 1:M character mappings, whereas the Character case mapping methods cannot.
Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the toUpperCase(int) method.
and
toTitleCase
Converts the character argument to titlecase using case mapping information from the UnicodeData file. If a character has no explicit titlecase mapping and is not itself a titlecase char according to UnicodeData, then the uppercase mapping is returned as an equivalent titlecase mapping. If the char argument is already a titlecase char, the same char value will be returned. Note that Character.isTitleCase(Character.toTitleCase(ch)) does not always return true for some ranges of characters.
Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the toTitleCase(int) method.
Then I tried to test them like this:
public class Test {
public static void main(String... args) {
String originalString = "abcdefghijklmnopqrstuvwxyz123546-.,/*&%+";
StringBuilder upperCaseStringBuilder = new StringBuilder();
StringBuilder titleCaseStringBuilder = new StringBuilder();
for (int i = 0; i < originalString.length(); i++) {
upperCaseStringBuilder.append(Character.toUpperCase(originalString.charAt(i)));
titleCaseStringBuilder.append(Character.toTitleCase(originalString.charAt(i)));
}
System.out.println("Original String : " + originalString);
System.out.println("UpperCase result: " + upperCaseStringBuilder.toString());
System.out.println("TitleCase result: " + titleCaseStringBuilder.toString());
}
}
This is the output:
Original String : abcdefghijklmnopqrstuvwxyz123546-.,/*&%+
UpperCase result: ABCDEFGHIJKLMNOPQRSTUVWXYZ123546-.,/*&%+
TitleCase result: ABCDEFGHIJKLMNOPQRSTUVWXYZ123546-.,/*&%+
So I couldn't understand the difference between these two methods. As I said before, I used toTitleCase()
in my code to capitalize a String
.
Are there any key difference which I didn't consider and which may lead my code to behave other than expected in some special cases?
Note: I don't think this is duplicate of String capitalize - better way . Because in that question the issue is with the performance of string capitalizing, not with the upper and title cases of characters as in this question.
1 Answer. The isUpperCase( ) method is used to check whether the given character is in upper case or not. It returns Boolean data type. The toUpperCase( ) method is used to convert a character or string into upper case.
Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.
Return Value The toUpperCase(char ch)method returns the uppercase of the given character.
You can use Character#toUpperCase() for this. char fUpper = Character. toUpperCase(f); char lUpper = Character. toUpperCase(l);
Standard ASCII characters are so boring! Here's something more exciting:
System.out.println(Character.toTitleCase('dz')); // Dz
System.out.println(Character.toUpperCase('dz')); // DZ
Live demo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With