简体   繁体   English

如何在不使用复杂代码结构的情况下串联Java中的字符串?

[英]How to concatenate a String in Java without using a complex code structure?

I have the following java code: 我有以下Java代码:

String strTest = null;

for (AlternativeEntity alternativeEntity : msg.Guidance()
      .getAlternatives()) {

    strTest = strTest + alternativeEntity.getArrivalStation().getName() + ", ";

}

The output looks like this: 输出看起来像这样:

nullabc, xyz, oop, 

How can I solve this problem and very bad character format? 我该如何解决这个问题以及非常糟糕的字符格式? It would be great if I can create output like this: 如果可以创建如下输出,那就太好了:

abc, xyz, oop

Initialize strTest as: strTest初始化为:

String strTest = "";

Also, remove the last comma , 另外,删除最后一个逗号,

strTest=strTest.substring(0, strTest.length()-1);

You can use Guava 's Joiner#join(Iterable parts) . 您可以使用GuavaJoiner#join(Iterable parts) For example: 例如:

Joiner joiner = Joiner.on(", ").skipNulls();
String result = joiner.join(list);
System.out.println(result);

Here, all the elements of the list will be printed comma separated without any trailing commas. 在此,列表的所有元素将以逗号分隔打印, 而没有任何结尾的逗号。 Also, all the null elements will be skipped. 同样,所有null元素都将被跳过。

More info: 更多信息:

Initialize your string to "": 将您的字符串初始化为“”:

 String strTest = "";

Alternatively, you should use a StringBuilder: 另外,您应该使用StringBuilder:

 StringBuilder builder = new StringBuilder();

 for (AlternativeEntity alternativeEntity : msg.Guidance()
  .getAlternatives()) {

     builder.append(alternativeEntity.getArrivalStation().getName()).append(", ");

 }

This will produce better performance. 这将产生更好的性能。

Java provides StringBuilder class just for this purpose,its simple and easy to use.. Java为此提供了StringBuilder类,它简单易用。

    StringBuilder str = new StringBuilder("India ");

     //to append "Hi"
    str.append("Hi");

    // print the whole string
    System.out.println("The string is "+str)

the output will be : The string is India Hi 输出将是 :字符串是India Hi

click here to know more about StringBuilder class 单击此处以了解有关StringBuilder类的更多信息

Replace String strTest = null; 替换字符串strTest = null; by String strTest = ""; 通过String strTest =“”;

Change 更改

 String strTest = null;

to

String strTest = "";

why don't you use: 为什么不使用:

String strTest = "";

and at the end: 最后:

if(strTest.endsWith(", "))
   strTest = strTest.substring(0, strTest.length()-2);

Initialize String strTest=""; 初始化String strTest="";

For skipping the last comma',' Use outside For loop: 要跳过最后一个逗号','在For循环外使用:

strTest = strTest.substring(0,strTest.trim().length()-1);
String strTest = null;

for (AlternativeEntity alternativeEntity : msg.Guidance().getAlternatives()) {
  String name = alternativeEntity.getArrivalStation().getName();
  strTest = (strTest == null) ? name : strTest +  ", " + name;
}

If the list is long, you should use a StringBuilder rather than the String for strTest because the code above builds a fresh string on each iteration: far too much copying. 如果列表很长,则应使用StringBuilder而不是strTestString ,因为上面的代码在每次迭代时都会生成一个新的字符串:复制过多。

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

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