PDA

Click to See Complete Forum and Search --> : String length


leeckeat
Mar 12th, 2001, 05:10 AM
Hi,
How do i set a fix length for a particular string?
eg.
abc
defg
hifklmn

i want to make all i nsame length?

billrogers
Mar 12th, 2001, 10:19 AM
you could extend the string class, making your own class which you could add functionality to allow that, that is the best oo approach.

Mar 12th, 2001, 03:42 PM
String is a final class, which means it cannot be extended.

But keeping with billrogers' idea, you can make your own class, say "FixedString" which has a private String member and you only allow modification upto the original size of the FixedString through a setter (depending on your needs, filling the remaining spots with spaces).

I was about to write such a class when I realized you might want to use the existing StringBuffer class.

Wow
This much free time at work.
Usage:
FixedString fs = new FixedString("SomeString", maxLengthAsINTEGER);
Say you want them all to have a length of 7:
eg.
abc
defg
hifklmn
use:
FixedString fs1 = new FixedString("eg.", 7);
FixedString fs2 = new FixedString("abc", 7);
etc.
There are also constructors for a given integer length or a given string. The max length is determined by the length you originally set or the length of the original string, respectively.

Rather than play with javadoc, I'll just give you what I have now.

leeckeat
Mar 13th, 2001, 05:42 AM
thank a lot, i finally solve String and integer problem.
But i have one more question about date.

I retrieve a date from system date, how do i convert it to previouse month? (i need it to retrieve particular data to print report on 1st of every month).

note: firstly the date format is yyyymmdd to match the format in database, when display on report, i need to change to MMMyyyy format.