dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: Separate a five digit number into parts

  1. #1
    Join Date
    Sep 2006
    Location
    PAKISTAN
    Posts
    14

    Question Separate a five digit number into parts

    Suppose i have a five digit number. How can i convert this number so that this number is divided into five parts and then separated by three blank spaces.
    Suppose i have a number 12345 then i want output like that 1 2 3 4 5

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Separate a five digit number into parts

    First you have to convert the number to a String. Is the number a int or a long?
    Really doesn't make any difference.
    A quick & dirty way to convert an int to a String is: String s = ""+12345;
    Now write a loop using the charAt() or substring() method to pick up the characters one at a time from the string and output them with a following space(s), except for the last one.

    Where are the 3 blank spaces in your your posted example? It looks like there are 4, 1 between each pair of digits.
    Norm

  3. #3
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Separate a five digit number into parts

    Suppose i have a number 12345 then i want output like that 1 2 3 4 5
    There are a few ways a doing this, one way is to convert the number to a String and then index through the String getting successive characters (either by converting the string to a char array or by calling the charAt() method). Append each character to a StringBuilder object followed by a space character (except for the last character) and finally convert the StringBuilder object to a String by calling its toString() method.

  4. #4
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Separate a five digit number into parts

    Sorry Norm I hadn't spotted that you'd already replied with basically the same answer as I gave.

    A quick & dirty way to convert an int to a String is: String s = ""+12345;
    Although I can't say I'd recommned doing this, it's not much more of an effort to type String s = String.valueOf(12345); and is much better practice.

  5. #5
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,856

    Re: Separate a five digit number into parts

    I agree. I was too lazy to look it up so just typed in a Q&D answer.
    Actually I'd prefer: String s = Integer.toString(12345); since you are dealing with integers.
    Norm

  6. #6
    Join Date
    Feb 2008
    Posts
    966

    Re: Separate a five digit number into parts

    Too bad the op can't use LISP/Scheme:

    (explode 'str)

  7. #7
    Join Date
    Sep 2006
    Location
    Eastern, NC, USA
    Posts
    907

    Re: Separate a five digit number into parts


  8. #8
    Join Date
    Jun 2008
    Posts
    10

    Re: Separate a five digit number into parts

    The posting in the other forum is different!!

    Alan

  9. #9
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Separate a five digit number into parts

    Quote Originally Posted by norm
    Actually I'd prefer: String s = Integer.toString(12345); since you are dealing with integers.
    Fair point.

  10. #10
    Join Date
    May 2008
    Posts
    8

    Re: Separate a five digit number into parts

    U could try converting the int into a String. Then create a String array and use the split(" ") to assign each number to a slot in the array.

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Separate a five digit number into parts

    U could try converting the int into a String. Then create a String array and use the split(" ") to assign each number to a slot in the array
    First of all a regex of " " wouldn't work as the number doesn't have any spaces in it and so split(" ") would return an array containing just one element which would be the complete number. And secondly, why go to all the trouble of using a regex and split() when the String class has a toCharArray() method which would return an array containing each character in the String.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)