2

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.
e.g.
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)

5
  • 1
    Why creating new string? Why not just use regex? Commented Apr 8, 2012 at 7:09
  • First, please don't capitalize your Strings. It's confusing. Java's naming convention states that variables are lowercase. Commented Apr 8, 2012 at 7:11
  • I agree with vidit: use a regex for this, don't do it like this. Commented Apr 8, 2012 at 7:11
  • docs.oracle.com/javase/tutorial/essential/regex/intro.html Commented Apr 8, 2012 at 7:13
  • When I replace the substring with "@", it's not based on the actual characters in that substring. It's an indexing thing. I only ever have an initial and final index of which to replace with "@" between, so I don't currently see how regex could help Commented Apr 8, 2012 at 7:15

5 Answers 5

6

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

This does seem like a proper method, but still a mouthful of code. There's no short, even inefficient method? Otherwise, I'll switch to this for the apparent efficiency.
@AntiEarth: It's a single method of three lines, which you need in one place. You can then call it from anywhere you want.
I'm just wondering over how small the actual performing code can be. Thanks!
4

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();

Comments

1

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.

6 Comments

Using that append method would still involve a for loop, correct? I'm going for a 'cleanliness' in code, rather than efficiency.
@AntiEarth - Like Jon said, whichever of these options you choose, it boils down to one method which you then call many times. Your standards are just unrealistic if that doesn't cut it for you :)
@AntiEarth The loop will be there somewhere - if you want single-line cleanliness, you'll need your own method, unless you go with a bounded approach of substringing a fixed string.
Oh and +1 for summing up both of what Jon and I later put.
But what if there is a one-liner out there, waiting to be discovered? D:
|
1

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);

1 Comment

That's a nice one liner, but I can never be sure of the maximum amount of characters to replace sadly.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.