• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Left/Right Padding String with blank spaces or other characters

 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Marshal
Posts: 81617
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 81617
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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

     
    You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
    The new gardening playing cards kickstarter is now live!
    https://www.kickstarter.com/projects/paulwheaton/garden-cards
    reply
      Bookmark Topic Watch Topic
    • New Topic