Stern inaktivStern inaktivStern inaktivStern inaktivStern inaktiv
 

Hallo,

wenn folgende Meldung kommt (String concatenation '+' in loop; ... usually it is preferable to replace it with explicit calls to StringBuilder.append() or StringBuffer.append()

Dann kann man das vorhandene Coding relativ einfach umbauen.

 

Hier ein Beispiel:

Coding mit Warnung:

private String getRestOfSplit(String text) {
 StringTokenizer tokens = new StringTokenizer(text, " ");
 tokens.nextToken();// this will contain password
 String second = "";
 
 // loop through tokens
 while (tokens.hasMoreTokens()) {
 second = second + " " + tokens.nextToken();
 }
 
 //Log.d(TAG, "Text rest with trim is: " + second);
 return second.replaceFirst("^ *", "");
 }
Source code

Neues Coding:

 private String getRestOfSplit(String text) { //example input for text: "password do some things"
 StringTokenizer tokens = new StringTokenizer(text, " ");
 tokens.nextToken(); // this will contain "password", we don't need it so we don't assign it to a variable
 StringBuilder second = new StringBuilder(); //changed to StringBuilder; here we want "do some things"
 
 // loop through tokens
 while (tokens.hasMoreTokens()) {
 // second = second + " " + tokens.nextToken(); old coding
 second.append(" ").append(tokens.nextToken()); //new coding
 }
 
 //Log.d(TAG, "Text rest with trim is: " + second);
 //return second.replaceFirst("^ *", ""); old coding
 return second.toString().replaceFirst("^ *", ""); //new coding replace first blank with nothing (eliminate the space)
 }
Source code