Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Stupid java string padding question

Options
  • 19-08-2009 3:32pm
    #1
    Registered Users Posts: 6,465 ✭✭✭


    Haven't used java much, and struggling with something very basic.
    I need to pad some strings to a fixed length. As String.format() isn't recognised, I'm obviously on 1.4.2, which doesn't seem to have the Formatter object.

    I'm presuming there's some handy way of doing this that I'm missing? (rather than just looping and concatenating spaces?)

    Tried using a StringBuffer and setting its length, but that's not working out line I'd hoped.

    Any suggestions?


Comments

  • Registered Users Posts: 3,078 ✭✭✭onemorechance




  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Why do you want to pad it out? Are there particular values you want to use as your padding? What way are you creating or manipulating these Strings?

    I don't think there are any shortcuts readily available to pad out a String barring using byte arrays of fixed length. Is there a pressing need for good performance or why are loops not suitable?


  • Registered Users Posts: 6,465 ✭✭✭MOH



    Thanks, but I've been through that. Unless I've missed it, there's nothing helpful there?
    ronivek wrote: »
    Why do you want to pad it out? Are there particular values you want to use as your padding? What way are you creating or manipulating these Strings?

    I'm calling a web service, pulling certain node values out of the response, and writing those to a file to be read by a COBOL program, so they have to be formatted to the expected fixed length.
    I don't think there are any shortcuts readily available to pad out a String barring using byte arrays of fixed length. Is there a pressing need for good performance or why are loops not suitable?

    Nothing wrong with a loop, I just thought there'd be some kind of sprintf equivalent that I was missing, but that only seems to be in 1.5.


  • Registered Users Posts: 10,636 ✭✭✭✭28064212


    Well (assuming I've understood this right, something which I can't guarantee), one simple way to do it is to just concatenate the String being returned with a String of spaces, then call substring. So, assuming you wanted every string to be of length 30:
    String blank = "                              "; // 30 spaces
    value = (value + blank).substring(0, 30);
    

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    I'm often in old 1.4 code, and I use Apache StringUtils, it fleshes out a lot of the missing stuff in 1.4

    http://commons.apache.org/lang/api/index.html


  • Advertisement
  • Closed Accounts Posts: 20 boars.ie


    28064212 wrote: »
    String blank = "                              "; // 30 spaces
    value = (value + blank).substring(0, 30);
    

    Nice one, couldn't possibly be simpler,
    maybe if explicit blank declaration was avoided :)


Advertisement