Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java split method

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.

like image 715
sefor Avatar asked Jul 18 '26 06:07

sefor


1 Answers

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.

like image 189
Zereges Avatar answered Jul 20 '26 18:07

Zereges



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!