12

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.

er-han
  • 1,721
  • 8
  • 17
  • Isn't *"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."* already clear enough? – Tom Dec 19 '17 at 12:40
  • @Tom not for me, sorry. – er-han Dec 19 '17 at 12:48
  • Possible duplicate of [String capitalize - better way](https://stackoverflow.com/questions/1536277/string-capitalize-better-way) – Jim G. Apr 05 '18 at 19:12

2 Answers2

11

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.

Oliver Charlesworth
  • 252,669
  • 29
  • 530
  • 650
-4

I don't really see any "real" differences here and I think it's best not to overthink small details. I wanna point out this only line here which should help with the "issue" here.

In general, String.toUpperCase() should be used to map characters to uppercase. String case mapping methods have several benefits over Character case mapping methods.

Documentation String.toUpperCase(): https://www.w3schools.com/jsref/jsref_touppercase.asp

Someone else should comment if they have more inside information.

Ronnie Oosting
  • 1,196
  • 1
  • 11
  • 32
Tumeski
  • 1
  • 3