Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

Java equivalent to VB 'string' function (create string with repeating  characters)?

Thread view: 
ohaya - 17 Oct 2005 21:19 GMT
Hi,

Is there an equivalent in Java to the VB 'string' function, which
creates a string of 'x' repeating characters?

For instance, something like:

String s = vbEquivString("x", 6);    // yields s = "xxxxxx"

Thanks,
Jim
Vova Reznik - 17 Oct 2005 21:31 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> Thanks,
> Jim
char[]arr = nea char[size];
Arrays.fill(arr, 'x');
String s = new vbEquivString(arr);
or
StringBuffer sb = new StringBuffer(size);
for(int i=0; i<sb.capacity(); i++){
  sb.append('x');
}
String s = sb.toString();
Roedy Green - 17 Oct 2005 22:07 GMT
>String s = vbEquivString("x", 6);    // yields s = "xxxxxx"

The .intern is in case you are producing the same strings over and
over.
   /**
    * Produce a String of a given repeating character.
    *
    * @param c
    *        the character to repeat
    * @param count
    *        the number of times to repeat
    * @return String, e.g. rep('*',4) returns "****"
    */
   public final static String rep ( char c, int count )
       {
       char[] s = new char[ count ];
       for ( int i = 0; i < count; i++ )
           {
           s[ i ] = c;
           }
       return new String( s ).intern();
       } // end rep
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

ohaya - 17 Oct 2005 22:40 GMT
> >String s = vbEquivString("x", 6);      // yields s = "xxxxxx"
>
[quoted text clipped - 18 lines]
>         return new String( s ).intern();
>         } // end rep

Vova and Roedy,

Thanks again!!!

Jim
Thomas Hawtin - 19 Oct 2005 02:25 GMT
> The .intern is in case you are producing the same strings over and
> over.

>         return new String( s ).intern();

To me, interning doesn't seem like the safe thing to do.

If the method is called often for the same values, then the string
itself will almost certainly be short lived. Interning just adds an
expensive operation. The additional GC is also particularly expensive,
but will be difficult to spot with a profiler.

If the method is called once and the result kept, then it doesn't really
matter where the string is.

There is a quite fun way to avoid excessive overheads when creating
varying length string of the same character repeated. Recall that
String.substring shares the underlying char array.

    private String zeros = "0000";
    public static String zeros(final int len) {
        if (len < 0) {
            throw new IllegalArgumentException()
        }
        for (;;) {
            final String zeros = ThisClass.zeros;
            if (zeros.length >= len) {
                return zeros.substring(0, len);
            }
            ThisClass.zeros = zeros+zeros;
        }
    }

Technically this requires Java 5.0 in order to be thread-safe.

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Roedy Green - 19 Oct 2005 03:41 GMT
>There is a quite fun way to avoid excessive overheads when creating
>varying length string of the same character repeated. Recall that
>String.substring shares the underlying char array.

I used that technique in the left zero padding method. The catch is it
does to work for a general repeat char.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

ohaya - 20 Oct 2005 02:12 GMT
> >There is a quite fun way to avoid excessive overheads when creating
> >varying length string of the same character repeated. Recall that
> >String.substring shares the underlying char array.
>
> I used that technique in the left zero padding method. The catch is it
> does to work for a general repeat char.

Hi all,

I'm the original poster.  FYI, what I was really looking to find out
originally was if there was a 'built-in' function to do this in Java.
From the responses, I gather that there isn't, so I've tried a couple of
the suggested methods, which all seemed to have worked, so again,
thanks!

Jim


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.