Rohan
Rohan

Reputation: 188

Replacing \\u by \u in java string

I have a string which contains normal text and Unicode in between, for example "abc\ue415abc". I want to replace all occurrences of \\u with \u. How can I achieve this? I used the following code but it's not working properly.

String s = "aaa\\u2022bbb\\u2014ccc";
StringBuffer buf = new StringBuffer();
Matcher m = Pattern.compile("\\\\u([0-9A-Fa-f]{4})").matcher(s);
while (m.find()) {
    try {
        int cp = Integer.parseInt(m.group(1), 16);
        m.appendReplacement(buf, "");
        buf.appendCodePoint(cp);
    } catch (NumberFormatException e) {
    }
}
m.appendTail(buf);
s = buf.toString();

Please help. Thanks in advance.

Upvotes: 2

Views: 1757

Answers (4)

Rob Starling
Rob Starling

Reputation: 3908

Your initial string doesn't, in fact, have any double backslashes.

String s = "aaa\\u2022bbb\\u2014ccc";

yields a string that contains aaa\u2022bbb\u2014ccc, as \\ is just java string-literal escaping for \.

If you want unicode characters: (StackOverflow21028089.java)

import java.util.regex.*;
class StackOverflow21028089 {
    public static void main(String[] args) {
        String s = "aaa\\u2022bbb\\u2014ccc";
        StringBuffer buf = new StringBuffer();
        Matcher m = Pattern.compile("\\\\u([0-9A-Fa-f]{4})").matcher(s);
        while (m.find()) {
            try {
                // see example:
                // http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#appendReplacement%28java.lang.StringBuffer,%20java.lang.String%29
                int cp = Integer.parseInt(m.group(1), 16);
                char[] chars = Character.toChars(cp);
                String rep = new String(chars);
                System.err.printf("Found %d which means '%s'\n", cp, rep);
                m.appendReplacement(buf, rep);
            } catch (NumberFormatException e) {
                System.err.println("Confused: " + e);
            }
        }
        m.appendTail(buf);
        s = buf.toString();
        System.out.println(s);
    }
}

=>

Found 8226 which means '•'
Found 8212 which means '—'
aaa•bbb—ccc

If you want aaa\u2022bbb\u2014ccc, that's what you started with. If you meant to start with a string literal with aaa\\u2022bbb\\u2014ccc, that's this:

String s = "aaa\\\\u2022bbb\\\\u2014ccc";

and converting it to the one with single slashes can be as simple as @Overv's code:

s = s.replaceAll("\\\\u", "\\u");

though since backslash has a special meaning in regex patterns and replacements (see Matcher's docs) (in addition to java parsing), this should probably be:

s = s.replaceAll("\\\\\\\\u", "\\\\u");

=>

aaa\u2022bbb\u2014ccc

Upvotes: 1

Marek Dovina
Marek Dovina

Reputation: 394

From API reference: http://developer.android.com/reference/java/lang/String.html#replace(java.lang.CharSequence, java.lang.CharSequence)

You can use public

public String replace (CharSequence target, CharSequence replacement)
string = string.replace("\\u", "\u");

or

String replacedString = string.replace("\\u", "\u");

Upvotes: 3

Rahul Gupta
Rahul Gupta

Reputation: 5295

There is a contains method and a replace method in String. That being said

String hello = "hgjgu\udfgyud\\ushddsjn\hsdfds\\ubjn";

if(hello.contains("\\u"))
    hello.replace("\\u","\u");

System.out.println(hello);

It will print :- hgjgu\udfgyud\ushddsjn\hsdfds\ubjn

Upvotes: 1

user666
user666

Reputation: 492

Try this:

s = s.replace(s.indexOf("\\u"), "\u");

Upvotes: 1

Related Questions