美文网首页
String字符串连loop里不要用“+”

String字符串连loop里不要用“+”

作者: ae12 | 来源:发表于2019-08-23 15:42 被阅读0次

最近项目优化,有如下一段代码:

String readString = "";
            String currentLine;
 while ((currentLine = in.readLine()) != null) {
                readString += currentLine;
            }

lint检测粗warning:
Reports String concatenation in loops. As every String concatenation copies the whole String, usually it is preferable to replace it with explicit calls to StringBuilder.append() or StringBuffer.append().
stackoverflow中有问题解答:
https://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java

重点一句是:
but at what point do you switch from concat to builder?
回答:
At the point where you're concatenating in a loop - that's usually when the compiler can't substitute StringBuilder by itself.

相关文章

  • String字符串连loop里不要用“+”

    最近项目优化,有如下一段代码: lint检测粗warning:Reports String concatenati...

  • lua使用的一点建议

    string 拼接大量字符串 少用默认连接符号当需要拼接大量字符串的时候,少用默认的字符串连接,如果大量字符串连接...

  • String类

    String 正则表达式 string类型在字符串改变、和字符串连加时性能差 Stringbuffer 和Stri...

  • 字符函数

    字符串函数 string StringConcatenate( 任意类型参 + ..... ) 字串连接(该函数...

  • C++ String遍历、查找、替换、插入和删除

    string string的初始化,在C++中字符串是一种数据类型; 1:string的初始化,遍历,字符串连接。...

  • 操作符

    1.String类支持“+”和“+=”操作符:“+”在String运算中,意味着字符串连接。如果String类与非...

  • 60.Permutation Sequence-Leetcode

    基础回顾 String 头文件中必须包括 string的声明初始化 遍历 string与char* 字符串连接 s...

  • 前端面试题系列之-原生js篇

    隐式类型转换相关 字符串连接符与算数运算符 字符串连接符+:会把其他数据类型调用String()方法转换成字符串然...

  • java学习之String类型

    标签:Java 数据类型 String String 型数据 语法: 当字符串连接符“+”和数学计算的“+”在一起...

  • 字符串

    String类 1、字符串连接 2、字符串的比较 3、字符串搜索 4、提取子串 5、字符串的转换、替换和分割 St...

网友评论

      本文标题:String字符串连loop里不要用“+”

      本文链接:https://www.haomeiwen.com/subject/ssqtectx.html