• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Left/Right Padding String with blank spaces or other characters

 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've created a method

to left pad the string with blank spaces using String.format method. I'm using the same in my program to right justify numbers. Numbers start with 2 (single-digit) and increase in multiples of 2 (containing n no. of digits) with the spaces being inversely proportional to the increase in digits.

Here is the output for illustrative purposes:

When the iterations increase exponentially, say from 2^10 to 2^128, then the String.format() seems to be a tad slow. I would like to replace String.format with StringBuilder but not sure how to implement it.

[sidebar]My program does have another method that uses StringBuilder to manually add fixed-length spaces (text to the left of colon in output). Rest of the program code not posted for brevity and also my concern is with the padAgain(String, int) method shown. If required, will post the entire code for reasons of clarity.[/sidebar]

How do I use StringBuilder to left / right pad variable number of blank spaces to existing string?

Could any of the forum experts / members help with their suggestions.

Thanks,
Sudhir

 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can try all sorts of things with a String builder. You can replace a stretch of text, or delete and insert. You can get index of "items" and insert just before that. There is even a section in the Java Tutorials about it. Try it; it is quite easy.Note that most methods have return this; as their last line, so you can join lots of calls together with dots.
 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can try all sorts of things with a String builder. You can replace a stretch of text, or delete and insert. You can get index of "items" and insert just before that. There is even a section in the Java Tutorials about it. Try it; it is quite easy.Note that most methods have return this; as their last line, so you can join lots of calls together with dots.


Thank you for your suggestions and the sample code. Will certainly read up on Java Tutorials, implement the same in my program and revert for further clarifications, if any.

Regards,
Sudhir
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome Do tell us how it works.
 
Sudhir Srinivasan
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You’re welcome Do tell us how it works.

I've spent half a day trying to implement StringBuilder and met with partial success(in the sample output below, the 5 & 4-digit numbers have been padded correctly but not so the 3,2 and single-digit numbers). Would therefore appreciate your help again.

padAgain(String, int) method modified to

and its output


Code explained (what I'm attempting to do):
----------------------------------------------------
  • variable 'padNum' stores the maximum length of the exponent value, being 65536 of max. length 5
  • variable 'result' is a String representation of numbers starting with 2
  • for loop to iterate till max. length is reached
  • StringBuilder object to insert the String value at index 0. Within this, invoke the indexOf(String) by passing 'result' as a substring of itself and insert blank space at position 0 upto max.length - length of each number
  • finally append 'result' to make up the max. length and
  • return as String value to calling object


  • Thanks
    Sudhir


     
    Sudhir Srinivasan
    Ranch Hand
    Posts: 93
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sudhir Srinivasan wrote:
    Campbell Ritchie wrote:You’re welcome Do tell us how it works.

    I've spent half a day trying to implement StringBuilder and met with partial success(in the sample output below, the 5 & 4-digit numbers have been padded correctly but not so the 3,2 and single-digit numbers). Would therefore appreciate your help again.

    Got it! From the looks of it, I've unnecessarily complicated with multiple insert calls et al.

    padAgain(String, int) method successfully simplified to


    left pad numbers with blank spaces using StringBuilder object. The spaces can be replaced with special characters by using the appropriate StringBuilder method within the loop construct.

    Sudhir

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic