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
Answer from Raju Sharma on Stack OverflowThe correct way to do this is using a regex to match the entire unicode definition and use group-replacement.
The regex to match the unicode-string:
A unicode-character looks like \uABCD, so \u, followed by a 4-character hexnumber string. Matching these can be done using
\\u[A-Fa-f\d]{4}
But there's a problem with this:
In a String like "just some \\uabcd arbitrary text" the \u would still get matched. So we need to make sure the \u is preceeded by an even number of \s:
(?<!\\)(\\\\)*\\u[A-Fa-f\d]{4}
Now as an output, we want a backslash followed by the hexnum-part. This can be done by group-replacement, so let's get start by grouping characters:
(?<!\\)(\\\\)*(\\u)([A-Fa-f\d]{4})
As a replacement we want all backlashes from the group that matches two backslashes, followed by a backslash and the hexnum-part of the unicode-literal:
3
Now for the actual code:
String pattern = "(?<!\\\\)(\\\\\\\\)*(\\\\u)([A-Fa-f\\d]{4})";
String replace = "
3";
Matcher match = Pattern.compile(pattern).matcher(test);
String result = match.replaceAll(replace);
That's a lot of backslashes! Well, there's an issue with java, regex and backslash: backslashes need to be escaped in java and regex. So "\\\\" as a pattern-string in java matches one \ as regex-matched character.
EDIT:
On actual strings, the characters need to be filtered out and be replaced by their integer-representation:
StringBuilder sb = new StringBuilder();
for(char c : in.toCharArray())
if(c > 127)
sb.append("\\").append(String.format("%04x", (int) c));
else
sb.append(c);
This assumes by "unicode-character" you mean non-ASCII-characters. This code will print any ASCII-character as is and output all other characters as backslash followed by their unicode-code. The definition "unicode-character" is rather vague though, as char in java always represents unicode-characters. This approach preserves any control-chars like "\n", "\r", etc., which is why I chose it over other definitions.
Try using String.replaceAll() method
s = s.replaceAll("\u", "\");
This is a character set issue. Make sure you read the file with the correct character set.
In character set Windows-1252, bytes C2 96 are characters  (U+00C2: Latin Capital Letter A with Circumflex) and – (U+2013: En Dash).
In character set ISO 8859-1, byte 96 is undefined. Byte C2 is Â, same as for Windows-1252.
In character set UTF-8, bytes C2 96 is the encoding of Unicode code point U+0096 (<Start of Guarded Area> (SPA)).
In character set UTF-16BE, bytes C2 96 is the encoding of character 슖 (U+C296: not valid). In character set UTF-16LE, they would decode as character 雂 (U+96C2: Han Character).
The question code uses new String(byte[]), which uses the platform default character set, so it is unclear from the code alone which character set is being used.
Since it's running on Windows, and prints as –, it would appear to be using character set Windows-1252. As such, to replace the pair of characters that resulted from reading the file using that character set, use:
content.replace("\u00C2\u2013", "BadCharacter")
If the Java code had read the file using UTF-8, by calling new String(data, StandardCharsets.UTF_8), the code should be:
content.replace("\u0096", "BadCharacter")
FYI: UltraEdit likely opened the file using UTF-8, which is why it appeared to be an empty file. See "Unicode text and Unicode files in UltraEdit" to learn more about how UltraEdit handles Unicode files.
I am having trouble using Java to identify and replacing a specific character that appears in a text file I have. It is a non-printable character, but it seems that Java renders it as – when outputting to the console.
It seems to be this character: https://www.fileformat.info/info/unicode/char/c296/index.htm
I'll translate:
Joe said: "I have a square. It is a circle".
You've made conflicting statements. Is it a non-printable character, or is it 슖 which is perfectly printable (see? I just printed it), or is it something completely different, which shows up as 0xC2 96 in the file and you've immediately jumped to the conclusion that this must mean it is 슖 because that has unicode number 0xC296?
Before you call this a 'bad file', it's just an encoded file and you simply need to apply the proper encoding to it.
Whenever bytes turn into characters or vice versa, charset conversion is ALWAYS applied. You can't not do it. Thus, in new String(bytes), yup, charset conversion is applied. Which one? Well, the 'platform default', which is just a funny way of saying 'the wrong answer'. You never want playform default. Don't ever call new String(bytes), it's a dumb method you should never use.
Unfortunately, plain text files just do not have an encoding tagged along with the data. You can't read .txt files unless you already know what encoding you have. If you don't know, you can't read it, or, if you don't know but you have a pretty good idea of what's in the file, you can go into Sherlock Holmes mode and attempt to figure it out.
You've told java that it is encoded with 'platform default', whatever that might be, (looks like ISO-8859-1 or Win-1252), and you get garbage out, but that's because you specified the wrong encoding, not because 'java is bad' or 'the file is bad'. Just specify the right encoding and all is right as rain.
Open the file with a text editor of some renown (such as SublimeText, cot editor, notepad++, etc), and play around with the encoding value until the text makes sense.
You must use your human brain (this is pretty hard AI for a computer to do!) and look at the file and make an educated guess. For example, if I see Mç~ller in a file where it seems logical that these are last names of european origin, that probably said Müller, so now I can either try to backsolve (look up the hex sequence, and toss tha sequence + ü in a web search engine and that'll usually tell you what to do), or just keep picking encodings in my editor until I see Müller appear, and now I know.
Thus, if you've done that, and truly you ahve determined that 슖 makes sense, okay, backsolving for that, the only encoding that makes sense here is UTF-16BE. So, toss that in your editor, or use new String(thoseBytes, "UTF-16BE") and see if the stuff makes sense now.
In no case should you be doing what these other answers are suggesting which is that you read the file with the wrong encoding and then try to clean up the epic mess that results. That's a bit like that Mr Bean sketch for training to paint a house:
- Get a can of paint.
- Get a stick of dynamite.
- Put the can in the middle of the room.
- Light the dynamite.
- Put the dynamite in the can and all done!
... and then clean up the mess and fix all the areas the explosion didn't catch and put out the fire.
Or maybe just, ya know, skip the dynamite and just buy a paintroller instead.
Same here. Just decode those bytes the right way in the first place instead of scraping globs of paint and dynamite wrapper off the walls.
my_string.replaceAll("\\p{C}", "?");
See more about Unicode regex. java.util.regexPattern/String.replaceAll supports them.
Op De Cirkel is mostly right. His suggestion will work in most cases:
myString.replaceAll("\\p{C}", "?");
But if myString might contain non-BMP codepoints then it's more complicated. \p{C} contains the surrogate codepoints of \p{Cs}. The replacement method above will corrupt non-BMP codepoints by sometimes replacing only half of the surrogate pair. It's possible this is a Java bug rather than intended behavior.
Using the other constituent categories is an option:
myString.replaceAll("[\\p{Cc}\\p{Cf}\\p{Co}\\p{Cn}]", "?");
However, solitary surrogate characters not part of a pair (each surrogate character has an assigned codepoint) will not be removed. A non-regex approach is the only way I know to properly handle \p{C}:
StringBuilder newString = new StringBuilder(myString.length());
for (int offset = 0; offset < myString.length();)
{
int codePoint = myString.codePointAt(offset);
offset += Character.charCount(codePoint);
// Replace invisible control characters and unused code points
switch (Character.getType(codePoint))
{
case Character.CONTROL: // \p{Cc}
case Character.FORMAT: // \p{Cf}
case Character.PRIVATE_USE: // \p{Co}
case Character.SURROGATE: // \p{Cs}
case Character.UNASSIGNED: // \p{Cn}
newString.append('?');
break;
default:
newString.append(Character.toChars(codePoint));
break;
}
}
Actually the string is "pequeño".
String s = "peque\u00f1o";
System.out.println(s.length());
System.out.println(s);
yields
7
pequeño
i.e. seven chars and the correct representation on System.out.
I remember giving the same response last week, use org.apache.commons.lang.StringEscapeUtils.
Joao's answer is probably the simplest, but this function can help when you don't want to have to download the apache jar, whether for space reasons, portability reasons, or you just don't want to mess with licenses or other Apache cruft. Also, since it doesn't have very much functionality, I think it should be faster. Here it is:
public static String unescapeUnicode(String s) {
StringBuilder sb = new StringBuilder();
int oldIndex = 0;
for (int i = 0; i + 2 < s.length(); i++) {
if (s.substring(i, i + 2).equals("\\u")) {
sb.append(s.substring(oldIndex, i));
int codePoint = Integer.parseInt(s.substring(i + 2, i + 6), 16);
sb.append(Character.toChars(codePoint));
i += 5;
oldIndex = i + 1;
}
}
sb.append(s.substring(oldIndex, s.length()));
return sb.toString();
}
I hope this helps! (You don't have to give me credit for this, I give it to public domain)
Try this:
StringEscapeUtils.unescapeJava("Hall\\u00F6")
\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"), "¶").
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;
}
Match the escape sequence \uXXXX with a regular expression. Then use a replacement loop to replace each occurrence of that escape sequence with the decoded value of the character.
Because Java string literals use \ to introduce escapes, the sequence \\ is used to represent \. Also, the Java regex syntax treats the sequence \u specially (to represent a Unicode escape). So the \ has to be escaped again, with an additonal \\. So, in the pattern, "\\\\u" really means, "match \u in the input."
To match the numeric portion, four hexadecimal characters, use the pattern \p{XDigit}, escaping the \ with an extra \. We want to easily extract the hex number as a group, so it is enclosed in parentheses to create a capturing group. Thus, "(\\p{XDigit}{4})" in the pattern means, "match 4 hexadecimal characters in the input, and capture them."
In a loop, we search for occurrences of the pattern, replacing each occurrence with the decoded character value. The character value is decoded by parsing the hexadecimal number. Integer.parseInt(m.group(1), 16) means, "parse the group captured in the previous match as a base-16 number." Then a replacement string is created with that character. The replacement string must be escaped, or quoted, in case it is $, which has special meaning in replacement text.
String data = "This is\\u2019 a sample text file \\u2014and it can ...";
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);
System.out.println(buf);
If you can use another library, you can use apache commons https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringEscapeUtils.html
String dirtyString = "Colocaci\u00F3n";
String cleanString = StringEscapeUtils.unescapeJava(dirtyString);
//cleanString = "Colocación"
Encoding Unicode data in UTF-8 can not fail. All Unicode characters can be encoded in UTF-8, so there is no failure condition (except maybe lack of memory or similar things).
If you decode UTF-8, then it can fail when the input is not really UTF-8. In that case, trying to decode it with UTF-8 is the wrong approach and there's no way to "fix UTF-8" to do the right thing: you must choose the correct encoding.
Could you provide some sample input and code to demonstrate what exactly it is, you're having problems with?
There is no reason at all to avoid unicode encoding. The actual problems come with the poor old encodings.
Convert all your files in UTF-8 and start your application with the system property file.encoding set to UTF-8.
java -Dfile.encoding=UTF-8
Provide some more information about your context if you want a more detailed answer.