2012-11-29 67 views
-8

我有如下要求。我有美國的電話號碼(10)數字,我需要從中提取區號,前綴和號碼。在美國分裂美國電話號碼?

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

123 
456 
7890 

我該怎麼做?

謝謝!

+0

你可以做到這一點的嘗試! – Anirudha

+0

接受答案?我會很感激。 – hologram

回答

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

希望有幫助!

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 

希望有所幫助!

1

String str =「1234567890」;

System.out.println(str.substring(0,3)); 
    System.out.println(str.substring(3,6)); 
    System.out.println(str.substring(6)); 
1

你可以試試這個模式太:

(\d){3}(\d){3}(\d){4}$ 
相關問題