Results 1 to 2 of 2
  1. #1
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default Using PadRight but want to actually cut the string off at that length

    Hi all,

    I am using the PadRight method I found online in order to pad a string by 'x' number of characters to the right.
    here is the method:
    Java Code:
    public static String padRight(String s, int n) {
            return String.format("%1$-" + n + "s", s);
        }
    If my string is longer than the number of characters I want to pad it by, then I want the string to be cut short.

    This is my call to that method:
    Java Code:
    padRight(rs.getString(3), 11)
    but very occassionally the result of rs.GetString(2) is longer than the 11 characters I specify in the call.
    When this happens I want the string to me NO MORE than 11.

    Can anybody advise?

    Thanks and Regards,
    Matt

  2. #2
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    sorry peeps, I think I have worked it out.

    I altered the padRight method to include a substring:

    Java Code:
    public static String padRight(String s, int n) {
            return String.format("%1$-" + n + "s", s).substring(0, n);
        }
    it seems to work and I don't think I am breakig it by introducing that.

Similar Threads

  1. [SOLVED] String Length issue.
    By ChrisMayhew in forum New To Java
    Replies: 7
    Last Post: 01-11-2009, 03:16 AM
  2. what does num.length method does?
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 10:19 PM
  3. Integer length
    By jithan in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 03:35 PM
  4. Replies: 2
    Last Post: 03-25-2008, 05:49 AM
  5. Help with method length
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:29 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
  •