• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating Empty String with Dynamic size....  RSS feed

 
satya kiran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how i can i create an empty string with dynamic size.
For example:
I have Strings s1,s2,s3....
if size of string less than 50 i need to pad the string with blanks so that size will be equal to 50.
How can i do this??
Currently i am doing some thing like this
if (s1.length <50) {
Stringbuffer sb1 = new StringBuffer();
sb1.append(s1);
for (int i=0; i < 50-s1.length(); i++) {
sb1.append(new String(" "));
}
}

Is there any better approach??
Thanks in advance..
kiran
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know of course that Strings are immutable? You would probably be better off creating a static method that pads the string for you. Something like this:

Then to pad a string with spaces to a length of 50 just do something like this:
 
satya kiran
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Morris! This is of real hep for me..
Ramesh
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!