File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Padding strings to valid length

 
Bill Raterink
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to force 1) a string to be a certain length, & 2) if there is a ",1" or a ",2" they MUST be in the last 2 positions. In VB it would be similar to this:
TxtStr = left(txtStr & Space(40),40) then tack the ,1 or ,2 on the end.
Please don't stone me for the VB, I'm trying very hard to learn java. ; )
Thanks. -Bill.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Bill -
We only throw rubber stones here. They don't hurt!
The only way I know to do what you want is to create a StringBuffer with the required size. If you need a String object instead of a StringBuffer, then invoke the StringBuffer object's toString() method to return a String object.
(Hmm. My theory isn't working! The toString() method interprets the first null character as the end of the string. I hope someone else out there can help us both!)
Hope that helps!
And welcome to Java.
[ November 04, 2003: Message edited by: Jeff Bosch ]
 
chi Lin
Ranch Hand
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,
Combine Jeff's idea, please try the follow method,
it create a StringBuffer, pad the content to a
desired size, then call toString() to return a String
that is aligned to right.
HTH
 
Bill Raterink
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff/chi, Thanks for the help. Now that I think about it, a string BUFFER sounds like what I should be using ANYWAY. In COBOL (forgive me) a buffer implied I/O, which is what I'm after. Of course, objects hadn't been invented yet. We thought object was just unreadable code. HA!HA! Thanks again, -Bill.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic