0

I have created a test to verify sorting of column data in a table. The sorting test is working fine for all the columns except phone numbers. I am using swapping to sort them. The problem is each phone number have "-" in between them. Like 657-726-8272. This is making my test to fail.

How do I remove dashes from the phone numbers and store all the numbers to an array without the dashes? I don't wan't to replace the '-' with anything, but get rid of them, so that I will have it as 6577268272

My data is like

{657-726-8672, 647-726-8272, 667-776-8771, 257-736-8272}

I need it to be

{6577268672, 6477268272, 6677768771, 2577368272}

so that the sorted list will be

{2577368272,6477268272, 6577268672, , 6677768771}

Thanks in Advance

ToDo Girl
  • 29
  • 1
  • 5
  • 2
    Possible duplicate of [Remove all occurrences of char from string](https://stackoverflow.com/questions/4576352/remove-all-occurrences-of-char-from-string) – takendarkk Nov 08 '18 at 22:22

1 Answers1

1

Use String's replaceAll method and replace the - with "" (empty string).

String phoneNumber = "657-726-8272".replaceAll("-", "");
// result: 6577268272

Note that replaceAll takes a regular expression. You could replace all non-numeric characters with a blank string, which would also handle phone numbers of the format "+1 (303) 555-1234" like so:

String phoneNumber = "+1 (303) 555-1234".replaceAll("\\D+", "");
// result: 13035551234
Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99