[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: one line to generate n spaces?



For optimization's sake (def of optimztn depends on your goal), Go ahead and
extend StringBuffer?  

Call it MyReallyCoolAndAdvancedStringBuffer (a joke), then as you think of
more and more string util type methods, you can add them. 

-----Original Message-----
From: Kevin Leeds [mailto:kleeds@learn.net]
Sent: Thursday, November 15, 2001 2:31 PM
To: ajug-members@ajug.org
Subject: RE: one line to generate n spaces?



Let's say I am quite unsure what the maximum length might be.

I still wish I had a standard java method (1.1.8) I
could just call. I guess there probably isn't one.

Here is what I did to optimize the loop (pretty much).
I think it works fine. In practical terms I don't
really need the answer; this is for curiosity's sake.

public class StringUtils()
{
  private static StringBuffer pad = new StringBuffer("          ");  // ten
spaces

  public static String padding(int n)
  {
     while (pad.length()<n)
        pad.append(pad);                     // double it

     return pad.toString().substring(0,n);
  }
}


> -----Original Message-----
> From: Nicholas Frank [mailto:nicholas.frank@s1.com]
>
> Kevin,
> 	Keep and array of char[] with what you think may be the max amount
> of spaces needed then use the static string function:
>
> String copyValueOf(char[] data, int offset, int count)
>
> 	Pull out as many spaces as you would like.
> Nicholas Frank