• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2955
  • Last Modified:

Need to remove dashes ("-") from a String value provided by the user

Dear Fellow Java developers:

I am trying to develop a method that validates a number that is given by a user.  The number is nine characters in length, and allows the user to send it in either of the following formats:

1. xxxxxxxxx
2. xxx-xxx-xxx

The validation method needs to check to see if there are dashes ("-") within the number, and if it finds any, then it should remove all occurrenes, then check to see if the number is 9 characters in length, and then return that number back.  

Please help.
Sincerely;
Fayyaz
0
fsyed
Asked:
fsyed
  • 2
1 Solution
 
CEHJCommented:

s = s.replaceAll("-", "").trim();
if (s.length() == 9) {
   // valid
}

Open in new window

0
 
fsyedAuthor Commented:
Thanks for the quick reply!
0
 
CEHJCommented:
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now