I found three ways to generate a prefix of a given string:
String s = ""
for(char c : word){
String prefix = s + c;
}
or
StringBuilder sb = new StringBuilder()
for(char c : word){
sb.append(c);
String prefix = sb.toString();
}
or
for(int i = 1; i< s.length(); i++) {
prefix = s.substring(0,i);
}
Is there any clear performance difference between these approaches? I read that substring is quite performant as it uses System.arrayCopy
, I am also not sure how the standard concatenation operator is performing vs StringBuilder.
3