我有如下要求。我有美国电话号码 (10) 位,我需要从中提取区号、前缀和号码。
Ex: 1234567890
it should take 1234567890 and it should return 3 strings as below:
123
456
7890
我怎样才能做到这一点?
谢谢!
String areaCode = number.substring(0,3);
String prefix = number.substring(3,6);
String rest = number.substring(6);
希望有帮助!
import java.util.Scanner;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter the 10-digit phone number, e.g. 1234567890");
String phoneNum = Integer.toString(keyboard.nextInt());
String first = phoneNum.substring(0,3);
String second = phoneNum.substring(3,6);
String third = phoneNum.substring(6);
System.out.println(first);
System.out.println(second);
System.out.println(third);
Run Output:
A prompt appears asking for this:
Please enter the 10-digit phone number, e.g. 1234567890
You enter (for example) 1234567890.
Method takes the substrings, and prints them on its own separate line.
Final Output:
123
456
7890
希望有帮助!
你也可以试试这个模式:
(\d){3}(\d){3}(\d){4}$
字符串 str = "1234567890";
System.out.println(str.substring(0,3));
System.out.println(str.substring(3,6));
System.out.println(str.substring(6));