• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Need to shift right and pad with zeroes

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Is there a quick and dirty way to move a numeric value into a string, shift that value to the right and pad the string with zeroes?

Need to sort by size and the only way to show the sorted data correctly is if they all line up on the same columnar position ie:

0000001
0000002
0000544

ect

Thanks for any insights
bc
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prior to JDK 5, you can use DecimalFormat:

For JDK 5 or later, it's probably more useful to learn to use formatters as found in String.format(), PrintWriter.format(), and other methods:

The JDK 5 stuff is (a) shorter, (b) more complex to learn, (c) more powerful, and (d) useful in a wider range of situations. So I'd say it's worthwhile to spend some time learning.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest using StringUtils from http://jakarta.apache.org/commons/lang/

You can see the detailed methods here:
http://jakarta.apache.org/commons/lang/api/org/apache/commons/lang/StringUtils.html

It's an expanded version of String and has a lot of cool methods and I think you should look at leftpad() which has 3 variations and you probably want
public static String leftPad(String str,
int size,
char padChar)Left pad a String with a specified character.

example StringUtils.leftPad("bat", 5, 'z') = "zzbat"

You could use strip() to strip white characters from you're 1st, get the resulting size and then you know how much you need to pad.


If you want to use standard Java then I suggest doing the following steps 'manually'

use String.trim() to trim your white spaces.
subtract the size of your new string from the size of your old.
For you're padding you could use several variations one of which would be
have a constant string filled with 0's the max size of your field.

zeroPad = substring(MaxzeroFilled, 0, oldStringSize - trimmedStringSize)

then merge the 2 together:

rightJustNum = zeroPad + trimmedNumber;

You have to be careful with your sizes but this should work.
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for the excellent information, didn't know about these utiliies whatsoever, appreciate the help very much!

bc
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic