【问题标题】:split US phone number in java? [closed]在java中拆分美国电话号码? [关闭]
【发布时间】:2012-11-17 04:07:51
【问题描述】:

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

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

123
456
7890

我该怎么做?

谢谢!

【问题讨论】:

  • 接受答案?我会很感激的。

标签: java regex


【解决方案1】:
String areaCode = number.substring(0,3);
String prefix = number.substring(3,6);
String rest = number.substring(6);

希望有帮助!

【讨论】:

    【解决方案2】:
    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
    

    希望有帮助!

    【讨论】:

      【解决方案3】:

      字符串 str = "1234567890";

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

      【讨论】:

        【解决方案4】:

        你也可以试试这个模式:

        (\d){3}(\d){3}(\d){4}$
        

        【讨论】:

          猜你喜欢
          • 2011-05-22
          • 1970-01-01
          • 2012-05-22
          • 2010-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-14
          相关资源
          最近更新 更多