栅栏柱问题的优雅解决方案(带字符串)

发布于 2024-11-26 18:38:36 字数 671 浏览 1 评论 0原文

我指的是将 String 与中间的某个 String 连接起来,例如用句点分隔的句子,或用逗号连接参数列表。我知道您可以使用库,但有时这些库不能满足您的要求,例如当您想要生成要连接的短语时。到目前为止,我已经提出了两种解决方案,

StringBuffer sentence = new StringBuffer();
String period = "";
for ( int i = 0; i < sentences.length; i++ ) {
    sentence.append( period + sentences[i] );
    period = ". ";
}

它们都受到period的冗余重新分配的影响。还有

StringBuffer actualParameters = new StringBuffer();
actualParameters.append( parameters[0] );
for ( int i = 1; i < parameters.length; i++ ) {
    actualParameters.append( ", " + parameters[i] );
}

一种取消了重新分配,但看起来仍然没有吸引力。任何其他解决方案将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酷炫老祖宗 2024-12-03 18:38:36

有一个 Apache Commons Lang 中的一系列函数就是这样做的

如果您必须自己编写代码,我通常执行此类操作的方式如下:

StringBuilder sb = new StringBuilder();
for (String sentence : sentences) {
    if (sb.length() != 0) {
        sb.append(". ");
    }
    sb.append(sentence);
}

此版本允许句子是任何可迭代的(返回字符串)。另请注意使用 StringBuilder 而不是 StringBuffer

很容易将其概括为类似于 org.apache.commons.lang.StringUtils.join 的东西。

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.

伤感在游骋 2024-12-03 18:38:36

如果您至少有一个字符串,那么:

String join(String separator, String... strings)
{
    String s = strings[0];
    for (int i = 1; i < strings.length; i++) {
        s += separator + strings[i]; 
    }
    return s;
}

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;
}
痴者 2024-12-03 18:38:36

似乎是一个常见问题!

删除 StringBuilder 的最后一个字符?

这会导致类似的结果:

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

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());
乖不如嘢 2024-12-03 18:38:36
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);
  }
}

不要使用 StringBuffer 因为不必要的同步和“+”运算符,因为这会创建不必要的中间 String 对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文