14

I am wondering that why Character.toUpperCase/toLowerCase has no Locale parameter like String.toUpperCase/toLowerCase.

I have to first uppercase of a text that can be in Any language. I have 2 solutions:

  1. Use Character.toUpperCase

    String text = "stack overflow";
    StringBuilder sb = new StringBuilder(text);   
    
    sb.setCharAt(0, Character.toUpperCase(sb.charAt(0))); // No Locale parameter here.
    
    String out = sb.toString(); //Out: Stack overflow
    
  2. Use String.toUpperCase

    Locale myLocale = new Locale(locateId);
    
    String text = "stack overflow";
    String text1 = text.substring(0,1).toUpperCase(myLocale );
    String text2 = text.substring(1);
    
    String out = text1 + text2; // Out: Stack overflow
    

For my Locale. Both way has the same result.

My question is:

  • Since the text can be in any language. Which way should I use?

  • Why Character.toUpperCase/toLowerCase has no Locale parameter because there is not much difference between Character.toUpperCase/toLowerCase and String.toUpperCase/toLowerCase because String is array of Characters.

msrd0
  • 7,816
  • 9
  • 47
  • 82
LHA
  • 9,398
  • 8
  • 46
  • 85

4 Answers4

7

As the Javadoc says:

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.

So use String.toUppercase()

dkatzel
  • 31,188
  • 3
  • 63
  • 67
6

From the Character#toUpperCase(int) Javadoc,

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.

So, the answer is your second example (String.toUpperCase)

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • beat me by 6 seconds! – dkatzel Oct 22 '14 at 19:04
  • So I should not use Character.toUpperCase/toLowerCase since my text can be in any language. Thanks! – LHA Oct 22 '14 at 19:08
  • Do Character.toUpperCase('i') always run Locale.US or systemLocale? Say running `i -> I` in US locale but turkish gives `i -> \u0130` according to String Javadocs. – Whome Nov 29 '15 at 18:29
2

Here I'll explain some of the issues involved with supporting a ToUpperCase and ToLowerCase version for chars, in addition to Strings, that a locale parameter would not solve:

  1. It's possible for an upper- or lower-case version of a char to be a string spanning more than one char. An example is the German ß expanding to SS in its upper-case version.

  2. A char is a 16-bit value, but not all Unicode characters (and not even all Unicode letters) can fit in a 16-bit char. Thus, a char version of ToUpperCase and ToLowerCase will not support the entire Unicode character repertoire.

  3. Some case mapping conversions (such as the Greek sigma) depend on context; that is, what kinds of characters come before or after the character in question. A char version of ToUpperCase and ToLowerCase will not have this kind of context available.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
1

If the question is "which method should I use", then this question is a duplicate of String conversion to Title Case and the correct answer is EITHER

If the question is "why doesn't Character have locale-sensitive case-changing methods", then the only way you're likely to get an answer is to consult one of the designers of the Java language. It's unlikely that the Stack Overflow community will be able to give you the answer you want.

Community
  • 1
  • 1
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Actually. I have 2 sub-questions as you know. Which way to use - I have the answer from Elliott Frisch and dkatzel. Other question why Character.toUpperCase/toLowerCase has no Locale parameter - I am asking this because maybe there is another reason that Locale is not needed. – LHA Oct 22 '14 at 19:23
  • Yes, that "other question" is what my last paragraph addresses. In future though, if you have two questions, you should ask two questions. It makes accepting an answer easier, if different answers are "best" for each subquestion. – Dawood ibn Kareem Oct 22 '14 at 19:25
  • Definitely Locale is needed to change the case of an individual character. For example, the result of converting `i` to upper case is different between English and Turkish. – Dawood ibn Kareem Oct 22 '14 at 19:27
  • Yes. I had the same thought but not sure why Character has no API for it. – LHA Oct 22 '14 at 19:29
  • Like my answer says, you'd have to consult one of the designers of Java. This is beyond the abilities of Stack Overflow to answer. – Dawood ibn Kareem Oct 22 '14 at 19:30