How to display Arabic unicode text in page that retrieved from database

3.5k views Asked by At

I need your help in displaying some Arabic text which is stored in a variable in the xhtml page. I have configured my project in jdeveloper to include UTF-8 in the properties and the Arabic text is displayed correctly. I have a variable called bankName and it has the unicode value which is :

String bankName = "\u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u062a\u062d\u062f";

in the xhtml when I am printing the variable output <h:outputText value="#{hrd.bankName}" style="font-weight:bold" />, the Arabic text is showing properly "البنك المتحد", however the same value of the unicode is stored in a database field and I am retrieving the value of it from the below code:

String bankName=result.getString("bank_name_arabic").trim();

The xhtml will display the Arabic text as a text:

 \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u062a\u062d\u062f

in the xhtml page and will not give the value of it in Arabic.

So how can I achieve this.

2

There are 2 answers

6
Adriano Repetti On BEST ANSWER

This line:

String bankName = "\u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u062a\u062d\u062f";

Is completely equivalent to this:

String bankName = "البنك المتحد";

Escaping (think, for example, about \n) isn't a mechanism in-built in Java strings. It's Java compiler that performs these replacements for you. Imagine to have a text file with these two characters: \ and n. If you read them like this:

Path path = Paths.get("yourFile.txt");
String text = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);

Now you can check text content: it's exactly two characters: \ and n then:

System.out.format("Content is '%s'", text);

Will print:

Content is '\n'

After this introduction you should understand why it doesn't work what you're doing: you're storing an escaped string in your database column but, as we said, unescaping is performed only by compiler and you get exactly what you stored.

If you can I'd suggest to simply store "البنك المتحد" in your database column.

If you can't change that and you ALREADY USE Apache Common Lang you can use an utility function to perform unescaping:

String bankName = org.apache.commons.lang.StringEscapeUtils.unescapeJava(
    getString("bank_name_arab‌​ic").trim());

Of course I wouldn't suggest to use it only for this (it's all but small), if you need it check tchrist's answer in How to unescape a Java string literal in Java.

1
Aleksander Panayotov On

In java, some characters have special meaning. You can find more information about those here: Java Characters Having that said, your string should become:

\\\\u0627\\\\\u0644\\\\\u0628\\\\\u0646\\\\\u0643 \\\\\u0627\\\\\u0644\\\\\u0645\\\\\u062a\\\\\u062d\\\\\u062f

The backslash \ is the symbol which is used to escape characters. That makes it a special symbol as well, so basically to pring a \, you need to have \\\\. Why 4? Because the first two represent a single \, which is used to escape the following single \, which is also represented by double backslashes. HTH