• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3476
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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