Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Q about the String class

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Q about the String class

    Hi there.

    Is there any method in the String class that will take 1234567899 and return - 123-456-7899 ?
    Reason: get a user input for phone number and return it as a readable phone number format.

    Thanks for you help!


  2. #2
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Q about the String class

    I'm a beginner myself, but I'd probably read the string as a set of char's, and say after 3 char's add '-', after another 3 char's add '-'....

  3. The Following User Says Thank You to MXA92 For This Useful Post:

    raabhim (March 23rd, 2012)

  4. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Q about the String class

    yup, it's working. Thank you!

     
     public String getPhoneNumber(String p){
            return p.charAt(0) + p.charAt(1) + p.charAt(2)+  "-" +
            p.charAt(3) + p.charAt(4) + p.charAt(5) + "-" +      
            p.charAt(6) + p.charAt(7) + p.charAt(8) + p.charAt(9);
     
     
        }
    seems like a mess though. is there a shortcut?
    Last edited by raabhim; March 23rd, 2012 at 01:42 PM.

  5. #4
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Q about the String class

    You could always check out String.format();


    EDIT:

    for example

    String s = "TestString";
    System.out.println(s.format("%s.%s", s.substring(0, 4), s.substring(4, s.length())));

    that would output

    Test.String
    Last edited by KucerakJM; March 23rd, 2012 at 02:09 PM.

  6. The Following User Says Thank You to KucerakJM For This Useful Post:

    raabhim (March 23rd, 2012)

  7. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    7
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Q about the String class

    just what I needed.
    Thank you very much!

Similar Threads

  1. [jsp/jsf] Send string(file name) to other class
    By barni120 in forum JavaServer Pages: JSP & JSTL
    Replies: 13
    Last Post: February 10th, 2012, 07:48 AM
  2. Question on String class
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: September 25th, 2011, 08:12 PM
  3. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  4. making text editor using string class
    By petadeer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2011, 03:28 AM
  5. Getting a string value from within a method in another class
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2010, 04:17 PM