IDEA-165193 Provide a quick-fix to replace String concatenation in loop with StringBuilder

This commit is contained in:
Tagir Valeev
2016-12-08 15:55:26 +07:00
parent de76e7ed3a
commit e7e0982841
15 changed files with 457 additions and 19 deletions

View File

@@ -0,0 +1,14 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
StringBuilder res = new StringBuilder();
for (String s : strings) {
if (res.length() > 0) {
res.insert(0, ".");
}
res.insert(0, s);
}
return res.toString();
}
}

View File

@@ -0,0 +1,15 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
StringBuilder res = null;
for (String s : strings) {
if(res == null) {
res = new StringBuilder(s);
} else {
res.append(s);
}
}
return res.toString();
}
}

View File

@@ -0,0 +1,15 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
void test(String[] strings) {
StringBuilder res = new StringBuilder();
for (String s : strings) {
if(res.length()+s.length() > 80) {
System.out.println(res);
res = new StringBuilder();
}
res.append(s);
}
System.out.println(res);
}
}

View File

@@ -0,0 +1,11 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
StringBuilder res = new StringBuilder();
for (String s : strings) {
res.append(s);
}
return res.toString();
}
}

View File

@@ -0,0 +1,12 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
StringBuilder res = new StringBuilder();
for (String s : strings) {
res.append(s);
}
res = new StringBuilder(res.toString().trim());
return (res.length() == 0) ? null : res.toString();
}
}

View File

@@ -0,0 +1,15 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
/*within*/
StringBuilder res = new StringBuilder();
for (String s : strings) {
if (/*before*/res.length() > 0) {
res.append(", ");
}
res.append(s);
}
return res.toString();
}
}

View File

@@ -0,0 +1,14 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
String res = "";
for (String s : strings) {
if (!res.isEmpty()) {
res = "." + res;
}
res = s <caret>+ res;
}
return res;
}
}

View File

@@ -0,0 +1,15 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
String res = null;
for (String s : strings) {
if(res == null) {
res = s;
} else {
res<caret>+=s;
}
}
return res;
}
}

View File

@@ -0,0 +1,15 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
void test(String[] strings) {
String res = "";
for (String s : strings) {
if(res.length()+s.length() > 80) {
System.out.println(res);
res = "";
}
res<caret>+=s;
}
System.out.println(res);
}
}

View File

@@ -0,0 +1,11 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
String res = "";
for (String s : strings) {
res <caret>+= s;
}
return res;
}
}

View File

@@ -0,0 +1,12 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
String res = "";
for (String s : strings) {
res <caret>+= s;
}
res = res.trim();
return res.isEmpty() ? null : res;
}
}

View File

@@ -0,0 +1,14 @@
// "Change type of 'res' to StringBuilder" "true"
public class Main {
String test(String[] strings) {
String res = "";
for (String s : strings) {
if (/*before*/!res/*within*/.isEmpty()) {
res += ", ";
}
res <caret>+= s;
}
return res;
}
}