Elegant Solutions to the Fencepost Problem (with Strings)
What I'm referring to is concatenating String
s with a certain String
in the middle, such as concatenating sentences separated by a period, or parameter lists with a comma. I know you can use libraries, but sometimes these can't do what you want, like when you want to generate the phrases you are concatenating. So far I've come up with two solutions,
StringBuffer sentence = new StringBuffer();
String period = "";
for ( int i = 0; i < sentences.length; i++ ) {
sentence.ap开发者_开发百科pend( period + sentences[i] );
period = ". ";
}
which suffers from the redundant reassignment of period
. There is also
StringBuffer actualParameters = new StringBuffer();
actualParameters.append( parameters[0] );
for ( int i = 1; i < parameters.length; i++ ) {
actualParameters.append( ", " + parameters[i] );
}
which removes the reassignment but which still looks unappealing. Any other solutions are greatly appreciated.
There is a family of functions in Apache Commons Lang that does just that.
If you have to code it yourself, the way I usually do this sort of thing is as follows:
StringBuilder sb = new StringBuilder();
for (String sentence : sentences) {
if (sb.length() != 0) {
sb.append(". ");
}
sb.append(sentence);
}
This version permits sentences
to be any iterable (returning strings). Also note the use of StringBuilder
instead of StringBuffer
.
It is easy to generalize this to something akin to org.apache.commons.lang.StringUtils.join
.
If you have at least one string then:
String join(String separator, String... strings)
{
String s = strings[0];
for (int i = 1; i < strings.length; i++) {
s += separator + strings[i];
}
return s;
}
Seems like a common question!
Remove last character of a StringBuilder?
That would lead to something like:
StringBuffer sentence = new StringBuffer();
String separator = ", ";
for ( int i = 0; i < sentences.length; i++ ) {
sentence.append( sentences[i] )
sentence.append( separator );
}
sentence.setLength(sentence.length() - separator.length());
public String join(String sep, String... parts) {
boolean first = true;
final StringBuilder sb = new StringBuilder();
for(String part: parts) {
if(first)
first = false;
else
sb.append(sep);
sb.append(part);
}
}
Don't use StringBuffer because of unnecessary synchronisation and "+" operator, because this will create unnecassry intemediate String objects.
精彩评论