String replacement function does not work in android

I used the following code to replace the appearance of '\', but it does not work.

msg="\uD83D\uDE0A"; msg=msg.replace("\\", "|"); 

I spent a lot of time on Google. But did not find any solution.

Also tried

 msg="\uD83D\uDE0A"; msg=msg.replace("\", "|"); 
+6
source share
3 answers

The msg line should also use an escape character like this:

 msg="\\uD83D\\uDE0A"; msg=msg.replace("\\", "|"); 

This code will work and it will result in: |uD83D|uDE0A

+4
source

If you want to show the integer unicode value of a unicode character, you can do something like this:

 String.format("\\u%04X", ch); 

(or use "|" instead of "\\" if you want).

You can go through each character in a string and convert it to a literal string, for example "|u####" , if that is what you want.

0
source

From what I understand, you want to get a unicode representation of a string. For this you can use the answer here .

 private static String escapeNonAscii(String str) { StringBuilder retStr = new StringBuilder(); for(int i=0; i<str.length(); i++) { int cp = Character.codePointAt(str, i); int charCount = Character.charCount(cp); if (charCount > 1) { i += charCount - 1; // 2. if (i >= str.length()) { throw new IllegalArgumentException("truncated unexpectedly"); } } if (cp < 128) { retStr.appendCodePoint(cp); } else { retStr.append(String.format("\\u%x", cp)); } } return retStr.toString(); } 

This will give you the unicode value as a string, which you can then replace as you like.

0
source

Source: https://habr.com/ru/post/988708/


All Articles