简体   繁体   中英

Java - Generating strings of length x

I have some 'heavy' string manipulation in my Java program, which often involves iterating through a String and replacing certain segments with filler characters , usually "@". These are characters are later removed but are used so that the length of the String and the current index are kept intact during the iteration.

This process usually involves replacing more than 1 character at a time.
eg
I might need to replace "cat" with "@@@" in the string "I love cats" , giving "I love @@@s" , So often I need to create strings of "@" with x length.

In python, this is easy.

NewString = "@" *x

In Java, I find my current method revolting.

String NewString = "";
for (int i=0; i< x; i++)  {  
    NewString = NewString.concat("@");  }

Is there a proper, pre-established method for doing this?
Does anybody have a shorter, more 'golfed' method?
Thanks!


Specs:
Java SE (Jre7)
Windows 7 (32)

It's not clear to me what kind of regex the comments are suggesting, but creating a string filled with a particular character to the given length is pretty easy:

public static String createString(char character, int length) {
    char[] chars = new char[length];
    Arrays.fill(chars, character);
    return new String(chars);
}

Guava has a nice little method Strings.repeat(String, int) . Looking at the source of that method, it basically amounts to this:

StringBuilder builder = new StringBuilder(string.length() * count);
for (int i = 0; i < count; i++) {
  builder.append(string);
}
return builder.toString();

Your way of building a string of length N is very inefficient. You should either use StringBuffer with its convenient append method, or build an array of N characters, and use the corresponding constructor of the String .

Can you always use the same characters in the "filler" String and do you know the maximum value of x ? The you can create a constant upfront which can be cut to arbitrary length:

private static final FILLER = "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@";

// inside your method
String newString = FILLER.substring(0, x);

java.lang.String is immutable. So, concating strings would result in creation of temporary string objects and thus is slow. You should consider using a mutable buffer like StringBuffer or StringBuilder . Another best practice when working with strings in java is to prefer using CharSequence type wherever possible. This would avoid unnecessary calls to toString() and you can easily change the underlying implementation type.

If you are looking for a one liner to repeat strings and this justifies using an external library, have a look at StringUtils.repeat from Apache Commons library. But, I feel you can just write your own code than using another library for a trivial task of repeating strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM