Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Stupid java string padding question

  • 19-08-2009 03:32PM
    #1
    Registered Users, Registered Users 2 Posts: 6,475 ✭✭✭


    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, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance




  • Registered Users, Registered Users 2 Posts: 1,919 ✭✭✭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, Registered Users 2 Posts: 6,475 ✭✭✭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, Registered Users 2 Posts: 11,087 ✭✭✭✭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, Registered Users 2 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