Results 1 to 3 of 3

Thread: String lengths

  1. #1
    Teclis is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default String lengths

    Say, a name is given-- last name only. The max allowed is 20 characters long. Is there a way to take the given name and add on spaces (" ") to the end of it to make it 20 characters long?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Yes.

    Look for useful methods in the String API docs.

    One method is suggested by the title you chose for the thread. (think for loops and string concatenation: foo+" "). Another (better but more sophisticated) comes from the fact that you are actually formatting the string to have a specific length.

  3. #3
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Try this:

    Java Code:
    while(name.length() < 20)
        name += " ";

Similar Threads

  1. Replies: 12
    Last Post: 11-26-2010, 09:33 AM
  2. Replies: 1
    Last Post: 11-04-2010, 07:53 PM
  3. Replies: 7
    Last Post: 11-03-2009, 03:18 AM
  4. Replies: 5
    Last Post: 01-11-2009, 11:12 AM
  5. Replies: 0
    Last Post: 11-20-2007, 05:59 PM

Posting Permissions

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