简体   繁体   English

java中的字符串连接

[英]String concat in java

I am trying to concat String array values in a String, The below code does not work我试图在字符串中连接字符串数组值,下面的代码不起作用

private void newString() {

    String str = "Split me"; 
    String[] tokens = str.split("[ ]+");

    String newStr = new String();

    for(int i=0; i<tokens.length; i++){
        newStr.concat(tokens[i]);
    }

    System.out.println("NEW STRING IS : " + newStr);
}


public static void main(String[] args){
    Main m = new Main();
    m.newString();
}

When you call newStr.concat(tokens[i]);当你调用newStr.concat(tokens[i]); this doesn't modify newStr , it returns a new String that is the result of the concatenation.不会修改newStr ,它返回一个新的String ,它是连接的结果。 You aren't doing anything with the result.你对结果没有做任何事情。

You need to do something like: newStr = newStr.concat(tokens[i]);您需要执行以下操作: newStr = newStr.concat(tokens[i]);

String is an immutable type in java, so it's methods work like this, returning a new String object. String是 java 中的不可变类型,所以它的方法是这样工作的,返回一个新的String对象。 Consider using a (mutable) StringBuilder instead.考虑使用(可变的) StringBuilder代替。 StringBuilder maintains an internal buffer that can grow, and so avoids the unnecessary String copies that come with using concatenation and immutable Strings . StringBuilder维护一个可以增长的内部缓冲区,因此避免了使用连接和不可变Strings带来的不必要的String副本。

String is immutable. String是不可变的。 Naturally, String.concat returns a brand new String .自然地, String.concat返回一个全新的String

... a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string. ... 创建一个新的String对象,表示一个字符序列,该字符序列是由这个String对象表示的字符序列和参数字符串表示的字符序列的串联。

The way you're trying to do it, the code should look like...你尝试这样做的方式,代码应该看起来像......

String glued = "";

for (final String token : tokens) {
  glued = glued.concat(token);
}

Now, the problem I have with this approach is that you're doing a lot of copies of the String data.现在,我用这种方法遇到的问题是你正在做很多String数据的副本。 Each concat copies all of the data in glued as well as the data in token .每个concat复制所有数据的glued以及在数据token Now, think about the implications of that... I'm not a fan of premature optimization, but I believe the logical way to achieve this is to instead use a StringBuilder as follows...现在,想想它的含义......我不喜欢过早优化,但我相信实现这一目标的合乎逻辑的方法是使用StringBuilder如下......

final StringBuffer buf = new StringBuffer();
for (final String token : tokens) {
  buf.append(token);
}
final String glued = buf.toString();

I try to fix it with StringBuilder,here is the new codes.我尝试用 StringBuilder 修复它,这是新代码。

private void newString() {
    String str = "Split me"; 
    String[] tokens = str.split("[ ]+");
    StringBuilder newStr = new StringBuilder();

    for(int i=0; i<tokens.length; i++){
        newStr.append(tokens[i]);
    }
    System.out.println("NEW STRING IS : " + newStr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM