Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

converting a String to UTF8 format

Tags:

java

string

I java code, I am having a string name = "örebro"; // its a swedish character.

But when I use this name in web application. I print some special character at 'Ö' character.

Is there anyway I can use the same character as it is in "örebro".

I did some thing like this but does not worked.

String name = "örebro";
byte[] utf8s = name .getBytes("UTF-8"); 
name = new String(utf8s, "UTF-8");

But the name at the end prints the same, something like this. �rebo

Please guide me

like image 554
jimmy Avatar asked Aug 07 '26 23:08

jimmy


1 Answers

The Java code you've provided is pointless, it will do nothing. Java Strings are already perfectly capable of encoding any character (though you have to be careful with literals in the source code, as they depend on the encoding the compiler uses, which is platform-dependant).

Most likely your problem is that your webpage does not declare the encoding correctly in the HTTP header or the HTML meta tags.

like image 148
Michael Borgwardt Avatar answered Aug 10 '26 12:08

Michael Borgwardt