3

I am trying to split an inputted number such as (123) 456-7890.

 String [] split = s.split(delimiters);

I have been searching the web for ways of delimiting the area code inside the set of the parentheses but I haven't found anything that works for my case. I do not know if the array is messing up with it printing either. The array is not required but I did not know what else to do since it is required to use the split method.

4
  • Your first splitted element is (123, so then you do System.out.println("(" + NumberTokens[0] + ")") you get two braces. Either add ( to delimeters and start from element 1 or just print without "(" Commented May 13, 2015 at 23:44
  • I need to make sure 123 is a token/element though and not (123. Commented May 13, 2015 at 23:45
  • Just remove all non-digit characters (simple regex) and process your number any way you like: System.out.println("123) 456-7890".replaceAll("\\D", "")); Commented May 13, 2015 at 23:55
  • Why did you remove all the details from your question? Commented May 14, 2015 at 2:22

4 Answers 4

1
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class HelloWorld{
    public static void main(String[] args){
        String phoneNumber = "(123)-456-7890";
        String pattern = "\\((\\d+)\\)-(\\d+)-(\\d+)";
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(phoneNumber);
        if (m.find())
            System.out.println(m.group(1) + " " + m.group(2) + " " + m.group(3));
     }
}

You can try it here.

Sign up to request clarification or add additional context in comments.

1 Comment

This does not answer the OP's question - as the OP requires the use of the split method. Also the phone number does not have a hyphen after the parenthesis: i is "(123) 456-7890" not "(123)-456-7890".
1

If I understand your question, you could use a Pattern like (XXX) XXX-XXXX where X is a digit. You can also use {n} to require n occurences. You group with (). Something like,

String str = "(123) 456-7890";
Pattern p = Pattern.compile("\\((\\d{3})\\) (\\d{3})-(\\d{4})");
Matcher m = p.matcher(str);
if (m.matches()) {
    String areaCode = m.group(1);
    String first3digits = m.group(2);
    String last4digits = m.group(3);
    System.out.printf("(%s) %s-%s%n", areaCode, first3digits,
            last4digits);
}

Gives your requested output of

(123) 456-7890

or, if you must use split you might first remove the ( and ) with a call to replaceAll and something like

String str = "(123) 456-7890";
String[] arr = str.replaceAll("[()]", "").split("[ -]");
System.out.printf("(%s) %s-%s%n", arr[0], arr[1], arr[2]);

which also gives your requested output of

(123) 456-7890

3 Comments

This does not answer the OP's question - as the OP requires the use of the split method.
you can split on the () so you don't need to replace them.
@igreen True, but you get two empty String(s) in the array. With my approach you do not.
0

this works:

    String s= "(123) 456-7890";
    String[] parts = s.split("[()\\- ]");
    System.out.println("(" + parts[1] + ") " + parts[3] + "-" + parts[4]);

Comments

0

If you must use the split method:

String s= "(123) 456-7890"
String[] split = s.split("[()-]");
System.out.println("(" + split[1] + ")" + split[2] + "-" + split[3]);

2 Comments

You don't need the \\ and a + after the pattern allows back-to-back delimiters to be captured.
The hyphen has special meaning between the square brackets.

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.