Elegant Solutions to the Fencepost Problem (with Strings)

我只是一个虾纸丫 提交于 2019-12-10 21:59:16

问题


What I'm referring to is concatenating Strings 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.append( 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.


回答1:


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.




回答2:


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



回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/6857502/elegant-solutions-to-the-fencepost-problem-with-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!