Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace a unicode character in java string

Tags:

java

unicode

I have an issue in my project that is replacing a unicode character with another unicode character in a Java string.

After searching and trying different codes, I didn't get the solution for that I want to replace a character in Persian to another character in Persian with a different unicode number.

I really appreciate any help.

Thanks

like image 845
user3173814 Avatar asked Apr 12 '14 00:04

user3173814


People also ask

How do you replace a special character in a string in Java?

Using String.replace() is used to replace all occurrences of a specific character or substring in a given String object without using regex. There are two overloaded methods available in Java for replace() : String. replace() with Character, and String. replace() with CharSequence.

How do I replace a character in a string?

replace() Method. This method returns a new string resulting from replacing all occurrences of old characters in the string with new characters.


1 Answers

Trying this will solve your issue.

str.replaceAll("\\p{Sc}", string_to_replcae);

and example can be like below:

String str = "For some reason my �double quotes� were lost.";
    str = str.replaceAll("\uFFFD", "\"");

this example can be viewed from here : How to replace � in a string

follow the link for more unicode expressions. http://www.regular-expressions.info/unicode.html

like image 55
Raju Sharma Avatar answered Oct 14 '22 03:10

Raju Sharma