To concatenate String we often use StringBuilder instead of String + String, but also we can do the same with String.format which returns the formatted string by given locale, format and arguments.
Examples:
Concatenate the string with StringBuilder
String concatenateStringWithStringBuilder(String name, String lName, String nick) {
final StringBuilder sb = new StringBuilder("Contact {");
sb.append(", name='").append(name)
.append(", lastName='").append(lName)
.append(", nickName='").append(nick)
.append('}');
return sb.toString();
}
Concatenate the string with StringFormat:
String concatenateStringWithStringFormat(String name, String lName, String nick) {
return String.format("Contact {name=%s, lastName=%s, nickName=%s}", name, lName, nick);
}
In performance, is String.Format as efficient as StringBuilder? Which one is better to concatenate strings and why?
UPDATE
I checked the similar question, but doesn´t answer my question. So far I have used StringBuilder to concatenate strings, should I follow it using? Or should I use String.format? the question is which one is better and why?