Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting ß.cfg to upper case using toUpperCase() in java

Tags:

java

I am trying following code

String s1 = "ß.cfg";
System.out.println (s.toUpperCase());

output I am getting is SS.CFG since Unicode didn't define an uppercase version of ß while I want the output as ß.CFG.

Is there any way I can achieve that?

like image 715
Umesh Awasthi Avatar asked Feb 03 '12 11:02

Umesh Awasthi


People also ask

What does the toUpperCase () method do?

Description. The toUpperCase() method returns the value of the string converted to uppercase. This method does not affect the value of the string itself since JavaScript strings are immutable.

Which method can be used to return a string in upper case letters uppercase () toUpperCase () TUC () toUpperCase () next?

Java String toUpperCase() Method The toUpperCase() method converts a string to upper case letters.

What does the following statement return Mystring toUpperCase ();?

Return Value It returns the String, converted to uppercase.


2 Answers

"ß" character is equivalent to "ss" (used in German, for example), and this is defined so in your Locale (the Locale you are using in your app).

You can try to do some experiment with a different Locale using method:

toUpperCase(Locale locale) 

Edit: As the user said, this method is not valid, a possible workaroud (not very elegant) is:

    String s1 = new String ("auß.cfg").replace('ß', '\u9999');
    System.out.println (s1.toUpperCase(Locale.UK).replace('\u9999', 'ß'));
like image 157
greuze Avatar answered Sep 23 '22 14:09

greuze


The documentation for toUpperCase( Locale ) explicitly states that this is what will happen:

Since case mappings are not always 1:1 char mappings, the resulting String may be a different length than the original String.

small letter sharp s -> two letters: SS

like image 41
tim_yates Avatar answered Sep 22 '22 14:09

tim_yates