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?

1
  • 1
    There's no "nothing character", you need the empty string as shown in the answer (pls accept it when it solved the problem). Commented Dec 15, 2019 at 1:44

1 Answer 1

0

You can use:

String p = StringUtils.replaceAll(phone, " ", "");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.