1

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.

99maas
  • 1,239
  • 12
  • 34
  • 59

2 Answers2

1

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.

Community
  • 1
  • 1
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208
  • I tried to keep the line which you have mentioned, it gave me error on org and on unescapeJava. So how to fix that error? I am using JDeveloper. Do I need to import any library? – 99maas Jun 16 '15 at 16:02
  • Yes it's a library but it's HUGE. If you're not already using that library I strongly suggest to use it just for this. See linked post for a possible implementation of that function. It's really well done. – Adriano Repetti Jun 16 '15 at 16:16
  • Typo: I strongly suggest to DO NOT use it just for this. – Adriano Repetti Jun 16 '15 at 16:17
  • So your final suggestion and recommendation is to store the text in Arabic in the database as I will be following your suggestion to create a master table and I will include the bank names in Arabic and english. Am I correct? – 99maas Jun 16 '15 at 16:33
  • Yes. If possible (no limitations about character set) then yes. No need (IMO) to play with escape sequences. It'll be harder to query, slower to use and bigger in size. They're useful in source code (they WERE even more) but no use outside that. – Adriano Repetti Jun 16 '15 at 16:38
  • I will make sure to follow your recommendation – 99maas Jun 16 '15 at 16:43
-1

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

Community
  • 1
  • 1