-2

I'm trying to take the a string and remove all the character from a char to another one

Example string:

 String personAndPhone = " Dave (206515608)"

I want to convert this string into two strings, the person name and the phone number (without the "()").

How can I do it?

Jonik
  • 80,077
  • 70
  • 264
  • 372

4 Answers4

2

Try this

String a[] = personAndPhone.trim().replaceAll("[()]", "").split(" ");

a[0] -> Dave
a[1] -> 206515608
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2

Use regex:

String name = str.replaceAll("\\s*(.*)\\s*\\(.*", "$1");
String phone = str.replaceAll(".*?\\((.*)\\).*","$1");

Both of these lines replace the entire input with group 1, which captures the target.

The "name" is everything up to the bracket, excluding whitespace at either end, so it would capture "John Smith 3rd" no problem.

The "phone" is everything between the first and last brackets, so it would capture "(01) 5678 9876" from " Dave ((01) 5678 9876) ".

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1

I notice you've got spaces around it, so the first thing I would do is trim.

personAndPhone = personAndPhone.trim();

Next, you've got a space in the middle, which might be a nice splitting point.

String[] tokens = personAndPhone.split(" ");

String name = tokens[0];
// now contains: Dave

Next, you can remove the brackets from the number:

String number = tokens[1].replaceAll("[()]", "");

// now contains: 206515608

Or as @EvgeniyDorofeev put more concisely:

String[] tokens = personAndPhone.trim().replaceAll("[()]", "").split(" ");
String name = tokens[0];
String number = tokens[1];
christopher
  • 26,815
  • 5
  • 55
  • 89
1

I'd be inclined to use regular expressions to do something like this: -

String personAndPhone = " Dave (206515608)"
Pattern pattern = Pattern.compile("([A-Za-z]+)\s\(([0-9]+)\)");
Matcher matcher = pattern.matcher(personAndPhone);

String name = matcher.group(1)
String number = matcher.group(2)

You can obviously make your regular expression a little more complicated as you need. This would allow you to parse being a little more strict. You also have a lot more control on precision and length checking with regular expressions.

See http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

ed_me
  • 3,338
  • 2
  • 24
  • 34
  • The name pattern is probably too simplistic... – assylias Aug 01 '13 at 10:56
  • Potentially, although I've mentioned and provided documentation to ways of making it more complex. Regular expressions aren't tricky to learn and spoon feeding isn't necessarily a good way to help out every one. This should allow the person asking the question to understand a different point of view for solving his problem and gives him/her a way of improving it for his own requirements. – ed_me Aug 01 '13 at 10:58
  • Could easily be `\s*(\w+)\s*\((\d+)\)` or even `\s*(\w+(?:\s\w+)?)\s*\((\d+)\)` for a first/last name. – Qix - MONICA WAS MISTREATED Aug 01 '13 at 11:00
  • Yes, there are a number of different patterns available for names although see http://stackoverflow.com/questions/888838/regular-expression-for-validating-names-and-surnames for an answer that highlights off nominal cases where names may fail regex checking. It all depends on the fidelity of testing and what is going to be required. – ed_me Aug 01 '13 at 11:04