String concatenation in loop
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().
Example:
String str = "";
for(int i=0; i<10; i++) {
str += i;
}After the quick-fix is applied:
String str = "";
StringBuilder strBuilder = new StringBuilder(str);
for(int i = 0; i<10; i++) {
strBuilder.append(i);
}
str = strBuilder.toString();- By ID
Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.
StringContatenationInLoop
Sometimes, the quick-fixes allow you to convert a String variable to a StringBuilder or introduce a new StringBuilder. Be careful if the original code specially handles the null value, as the replacement may change semantics. If null is possible, null-safe fixes that generate necessary null-checks are suggested. Also, it's not guaranteed that the automatic replacement will always be more performant.
You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:
//noinspection StringContatenationInLoopnote
Actual comment syntax will depend on the code language you are working with
More detailed instructions as well as other ways and options that you have can be found in the product documentation:
Inspection Details | |
|---|---|
By default bundled with: |