简体   繁体   English

在以下情况下使用StringBuilder或String?

[英]Use StringBuilder or String in the following case?

String address=null;
String body = "";
String date = "";

for(int i = 0; somecondition; i++)
{    
    body = cursor.getBody(i);
   //and so on all strings get changed

//REST OF THE CODE
}

This loops for arround 500 times so what should I use? 这会循环500次,所以我应该怎么用?

Given that nothing changes in the String you are just reassigning it, use a String. 鉴于String中没有任何更改,您只是在重新分配它,请使用String。 StringBuilder is used when you want to build up a String in pieces. 当您要分段构建String时使用StringBuilder。

Since you're just assigning some string to the variable, just use String instead of StringBuilder. 由于您只是为变量分配了一些字符串,因此只需使用String而不是StringBuilder。 StringBuilder as its name suggest, is used for building a new string where concatenation or manipulation is desired. 顾名思义,StringBuilder用于在需要连接或操作的地方构建新的字符串。

Your example doesn't build any String, just going assign values. 您的示例不构建任何String,而只是分配值。 In that case you can use String 在这种情况下,您可以使用String

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

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