Blank character constant in Java

Sourav Mehra :

How can we replace a particular char constant with blank in Java?

\0 replaces with space char and not with blank.

Input :

    String amount = "1.22.33,11";

Desired result : 12233,11

Ole V.V. :
    String amount = "1.22.33,11";
    String result = amount.replace(".", "");
    System.out.println(result);

Output:

12233,11

No need to use a regular expression nor an external dependency.

The replace​(CharSequence target, CharSequence replacement) method of the String class replaces every occurrence of the literal target string with the replacement string. So just give the empty string as replacement.

Your task may be described as removing occurrences of .. So replacing with a “blank” character, whatever that is, is not the right solution, you would still have some character there. Instead we replace a one-character string by another string that has no characters in it.

As @Lino pointed out in comments, JDK version up to at least 8 (I haven’t checked 9 nor 10) compile a regular expression into a Pattern for each invocation of the method. In Java 11 an efficient implementation has been provided.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=115223&siteId=1