How to replace certain characters of a string

4166 views java
3

  • I have a string @B1AdGODG7:Devrath%Dev\u00B6
  • I want to replace \u00B6 with ¶

I tried with

String comments="@B1AdGODG7:Devrath%Dev\u00B6";
comments=comments.replaceAll("\u00B6","¶");
  • Output: @B1AdGODG7:Devrath%Dev\u00B6

  • Required Output: @B1AdGODG7:Devrath%Dev¶


  • ReplaceAll is not working
  • How to make the required output

Snapshot:

enter image description here

answered question

When I try this on my machine it prints @B1AdGODG7:Devrath%Dev¶

Wrap the String in Html.fromHtml() and it'll parse for you. If it doesn't display properly, you don't have a supporting font.

Your code work for me

@GBlodgett and E. Betanzos .... Please check the snapshot I attached in debug mode

Your code works for me too

3 Answers

3

I get below example from http://techidiocy.com/replace-unicode-characters-from-java-string/ . I think it is work for u

public static StringBuffer removeUTFCharacters(String data){
Pattern p = Pattern.compile("\\\\u(\\p{XDigit}{4})");
Matcher m = p.matcher(data);
StringBuffer buf = new StringBuffer(data.length());
while (m.find()) {
String ch = String.valueOf((char) Integer.parseInt(m.group(1), 16));
m.appendReplacement(buf, Matcher.quoteReplacement(ch));
}
m.appendTail(buf);
return buf;
}

posted this
7

\u00B6 is a single character, with the Unicode code point of 0xB6; writing \u00B6 is literally the same as writing ¶.

So, you need to escape the backslash: \\. Furthermore, backslashes are special in regular expressions, which replaceAll uses, so you need to escape it again -- and that escape needs to be escaped: replaceAll("\\\\u00B6", "¶").

You could also use Pattern.quote for that second level of escaping (the one for the regex): replaceAll(Pattern.quote("\\u00B6"), "¶").

posted this
2

instead of using replaceAll() use replace() it will work fine.

posted this

Have an answer?

JD

Please login first before posting an answer.