-9

我有如下要求。我有美国电话号码 (10) 位,我需要从中提取区号、前缀和号码。

Ex: 1234567890
it should take 1234567890 and it should return 3 strings as below:

123
456
7890

我怎样才能做到这一点?

谢谢!

4

4 回答 4

5
String areaCode = number.substring(0,3);
String prefix = number.substring(3,6);
String rest = number.substring(6);

希望有帮助!

于 2012-11-29T06:03:23.483 回答
3
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

希望有帮助!

于 2012-11-29T06:13:52.090 回答
1

你也可以试试这个模式:

(\d){3}(\d){3}(\d){4}$
于 2012-11-29T06:49:24.707 回答
1

字符串 str = "1234567890";

    System.out.println(str.substring(0,3));
    System.out.println(str.substring(3,6));
    System.out.println(str.substring(6));
于 2012-11-29T06:26:12.363 回答