While working in a Java app, I recently needed to assemble a comma-delimited list of values to pass to another web service without knowing how many elements there would be in advance. The best I could come up with off the top of my head was something like this:
public String appendWithDelimiter( String original, String addition, String delimiter ) {
if ( original.equals( "" ) ) {
return addition;
} else {
return original + delimiter + addition;
}
}
String parameterString = "";
if ( condition ) parameterString = appendWithDelimiter( parameterString, "elementName", "," );
if ( anotherCondition ) parameterString = appendWithDelimiter( parameterString, "anotherElementName", "," );
I realize this isn't particularly efficient, since there are strings being created all over the place, but I was going for clarity more than optimization.
In Ruby, I can do something like this instead, which feels much more elegant:
parameterArray = [];
parameterArray << "elementName" if condition;
parameterArray << "anotherElementName" if anotherCondition;
parameterString = parameterArray.join(",");
But since Java lacks a join command, I couldn't figure out anything equivalent.
So, what's the best way to do this in Java?
In the case of Android, the StringUtils class from commons isn't available, so for this I used
http://developer.android.com/reference/android/text/TextUtils.html
Your approach is not too bad, but you should use a StringBuffer instead of using the + sign. The + has the big disadvantage that a new String instance is being created for each single operation. The longer your string gets, the bigger the overhead. So using a StringBuffer should be the fastest way:
After you have finished creating your string simply call toString() on the returned StringBuffer.
If you're using Eclipse Collections, you can use
makeString()
orappendString()
.makeString()
returns aString
representation, similar totoString()
.It has three forms
makeString(start, separator, end)
makeString(separator)
defaults start and end to empty stringsmakeString()
defaults the separator to", "
(comma and space)Code example:
appendString()
is similar tomakeString()
, but it appends to anAppendable
(likeStringBuilder
) and isvoid
. It has the same three forms, with an additional first argument, the Appendable.If you can't convert your collection to an Eclipse Collections type, just adapt it with the relevant adapter.
Note: I am a committer for Eclipse collections.
You're making this a little more complicated than it has to be. Let's start with the end of your example:
With the small change of using a StringBuilder instead of a String, this becomes:
When you're done (I assume you have to check a few other conditions as well), just make sure you remove the tailing comma with a command like this:
And finally, get the string you want with
You could also replace the "," in the second call to append with a generic delimiter string that can be set to anything. If you have a list of things you know you need to append (non-conditionally), you could put this code inside a method that takes a list of strings.
Apache commons StringUtils class has a join method.
So a couple of things you might do to get the feel that it seems like you're looking for:
1) Extend List class - and add the join method to it. The join method would simply do the work of concatenating and adding the delimiter (which could be a param to the join method)
2) It looks like Java 7 is going to be adding extension methods to java - which allows you just to attach a specific method on to a class: so you could write that join method and add it as an extension method to List or even to Collection.
Solution 1 is probably the only realistic one, now, though since Java 7 isn't out yet :) But it should work just fine.
To use both of these, you'd just add all your items to the List or Collection as usual, and then call the new custom method to 'join' them.