0

I want to replace "\u0023 \u0024 ab" with: "\\u0023 \\u0024 ab" to maintain its encoding before storing it in database.

There can me many different values after \u, not just 0023, 0024.

I tried using str.replace("\"\\"); or ("\u","\\u") but it doesn't work in java because it treats \u0023 as one character.

Any suggestions how to achieve this ?

I tried like this

public class test {
    public static void main(String args[]) {
        String s = "\u0023";       
        s = s.replace("\\", "\\\\");
        System.out.println(s);
    }
}

but it is giving following output as:
#

but I am expecting:
\\u0023
Bharthan
  • 1,458
  • 2
  • 17
  • 29
  • 1
    There is no difference between both... – Yassin Hajaj Oct 27 '15 at 22:59
  • 1
    If your source string is `"\u0023 \u0023 ab"`, the occurences of `\u0023` *are* one character, namely `#`. So your source string is `"# # ab"`… – Holger Oct 27 '15 at 22:59
  • Possible duplicate of [Replace # with \u0023 in a Java String](http://stackoverflow.com/questions/11791077/replace-with-u0023-in-a-java-string) – malaguna Oct 27 '15 at 23:00
  • 2
    Please edit your question and wrap your examples with `code samples`. Currently I am not sure how many ``\`` your string contains because Stack Overflow also uses ``\`` as special character and escapes it which means that ``\\`` becomes ``\\``. – Pshemo Oct 27 '15 at 23:05
  • *which means that ``\\`` becomes ``\``. – Pshemo Oct 27 '15 at 23:11
  • To update your question use [edit] option (it seems that there is one pending edit suggestion so first you will need to accept or reject it). To create `code sample` use `{}` button in editors menu. – Pshemo Oct 27 '15 at 23:12

2 Answers2

1

As said by the first answer here, you can retrieve the (numerical) unicode value of a character with:

// works up to Unicode 3.0
String hexString = Integer.toHexString(s | 0x10000).substring(1);

Using this number, simply print it out:

System.out.println("\u" + hexString);

(Note: code is untested: give me feedback if it doesn't work)

Hope this helps!

Community
  • 1
  • 1
Jonathan Lam
  • 16,831
  • 17
  • 68
  • 94
0
String s = "\\u0023";       
s = s.replace("\\", "\\\\");
Pshemo
  • 122,468
  • 25
  • 185
  • 269
MIK
  • 392
  • 3
  • 19
  • In his/her case: `"\u0023 \u0023 ab".replaceAll("\u0023", "\\\\u0023")` – k4yaman Oct 27 '15 at 23:04
  • 1
    Just indent your code with four spaces. – Holger Oct 27 '15 at 23:05
  • 1
    To avoid problems with ``\`` and `<...>` simply post your code as `code sample` (use `{}` button in editors menu). Don't post text as image. – Pshemo Oct 27 '15 at 23:06
  • there can be many different values after \u not not just 0023 – Bharthan Oct 27 '15 at 23:36
  • @Bharthan: this code doesn’t depend on the actual number. As you should realize, there is no occurrence of the number in the arguments to the `replace` call. – Holger Oct 27 '15 at 23:46
  • @Holger: it is replacing \\ with \\\\ but my string only have single \ – Bharthan Oct 27 '15 at 23:54
  • @Bharthan: do you *understand* Java code, i.e. escape sequences in strings? Or, alternatively, are you able to *try* it? – Holger Oct 27 '15 at 23:57
  • @MIK Like Bharthan is saying, I don't think this is answering the question: he has a `String` (unicode) representation of a character *with only one slash*. He wants to know how to print the unicode value with the given value with *only one slash*. – Jonathan Lam Oct 28 '15 at 01:39
  • @Jonathan: please, try to understand [this article](https://docs.oracle.com/javase/tutorial/java/data/characters.html), section “Escape Sequences” first. As I already said [here](http://stackoverflow.com/questions/33379875/how-to-manipulate-unicode-characters-in-java/33380013#comment54553799_33379875), an occurrence of `\u0023` is a single character, i.e. `#`. Since it gets interpreted by the compiler, you can’t dissolve that single character at runtime. You (and the OP) have to understand the difference between an actual string contents ant it’s compile-time representation. – Holger Oct 28 '15 at 08:31