2

I like to normalize the phone numbers I get from the contacts in the local phone book. To do that, I want to remove any spaces, dashes, plus signs etc from the number.

CN1 only offers the String.replace(oldchar, newchar) function, instead of String operations. From this post, How to represent empty char in Java Character class, this should be the way to go:

primaryPhoneNumber = primaryPhoneNumber.replace(' ', Character.MIN_VALUE);

however, this approach has several implications.

  1. the char in the console output looks like a space, but its not. its a string terminator.

+49 234-63446 0 234 63446

  1. when using this normalized string literal, including the Character.Min_Value in a database, the database query involving this string crashes: Caused by: org.postgresql.util.PSQLException: ERROR: invalid byte sequence for encoding "UTF8": 0x00

How to properly remove spaces and other chars and replace them with a "nothing" character?

Lequi
  • 539
  • 2
  • 13
  • 1
    There's no "nothing character", you need the empty string as shown in the answer (pls accept it when it solved the problem). – maaartinus Dec 15 '19 at 01:44

1 Answers1

0

You can use:

String p = StringUtils.replaceAll(phone, " ", "");
Shai Almog
  • 51,749
  • 5
  • 35
  • 65