Reputation: 1841
I have a line "Example: \u09b8\u09be\u09b0\u09cd\u09ac\u09bf\u09df\u09be\u09df \u0993\u09df\u09be\u09b0\u09cd\u0995\u09aa\u09be\u09b0\u09ae\u09bf\u099f \u09ad\u09bf\u09b8\u09be,"
What I want to, get a string from this line as follows : "Example: সার্বিয়ায় ওয়ার্কপারমিট ভিসা,"
I assume that it could be found by Integer.parseInt()
method, then what will be the procedure?
Upvotes: -1
Views: 81
Reputation: 340158
Your input is a String
literal containing escaped Unicode code point numbers. This syntax is understood directly by the Java compiler.
So there is no need to unescape.
String input = "ইউরোপের\u09b8\u09be\u09b0\u09cd\u09ac\u09bf\u09df\u09be\u09df \u0993\u09df\u09be\u09b0\u09cd\u0995\u09aa\u09be\u09b0\u09ae\u09bf\u099f \u09ad\u09bf\u09b8\u09be," ;
System.out.println( input ) ;
See this code run at Ideone.com.
ইউরোপেরসার্বিয়ায় ওয়ার্কপারমিট ভিসা,
Perhaps you want to generate Java source code without the escapes. That is legitimate as the Java language fully supports Unicode characters in source code.
We can add a double-quote character to each end of your string. Since Java syntax uses the double-quote character, we must escape like this: "\""
.
// Generate source code.
String sourceCode = "\"" + input + "\"" ;
System.out.println( sourceCode ) ;
"ইউরোপেরসার্বিয়ায় ওয়ার্কপারমিট ভিসা,"
Upvotes: 1
Reputation: 4292
If my comment HERE is correct then this approach is simpler:
Matcher m = Pattern.compile("\\\\u[0-9a-f]{4}").matcher(s);
StringBuilder sb = new StringBuilder();
while (m.find()) {
m.appendReplacement(sb, Character.toString(Integer.parseInt(m.group(), 16)));
}
m.appendTail(sb);
System.out.println(sb.toString());
Upvotes: 0
Reputation: 1841
The main approach for this method is
int hexValue = Integer.parseInt(letter, 16);
So, I have found the result with following functions :
public static String unicodeStringToString(String unicode){
System.out.println("ddd first Response 3:unicode " + unicode);
if(TextUtils.isEmpty(unicode)){
return "";
}
StringBuilder txt = new StringBuilder();
String [] words = unicode.split(" ");
for (String word: words){
if(word.contains("\\u")) {
System.out.println("ddd first Response 4:word " + word);
word = word.replace("\\", "");
String[] letters = word.split("u");
for (String letter : letters) {
System.out.println("ddd first Response 5:letter " + letter);
if(letter.contains(",")) {
letter= letter.replace(",", "");
String s = unicodeNumberToString(letter);
txt.append(s);
txt.append(",");
}else {
String s = unicodeNumberToString(letter);
txt.append(s);
}
txt.append(" ");
}
}else {
txt.append(word);
txt.append(" ");
}
}
return txt.toString();
// uses : textView.setText(Html.fromHtml(unicodeStringToString(unicode)));
}
public static String unicodeNumberToString(String letter){
if(isInteger(letter)) {
int hexValue = 0;
try {
hexValue = Integer.parseInt(letter, 16);
} catch (NumberFormatException e) {
e.printStackTrace();
}
letter = String.valueOf(((char) hexValue));
}
return letter ;
}
public static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
System.out.println("integer Problem: isInteger: "+e.toString());
return false;
}
return true;
}
Upvotes: -1