Junior — Middle
What ways of creating a string, other than simple concatenation, do you know?
sobes.tech AI
Answer from AI
In addition to simple string concatenation, there are other ways to create strings:
- String formatting — allows inserting values into a template. For example, in Python it's
f"{variable}", in Java —String.format(), in C# —$"{variable}". - Using StringBuilder (or equivalents) — efficient for multiple string additions, especially in loops, to avoid creating many intermediate objects.
- Join methods — combining a collection of strings with a separator, e.g.,
String.join(",", list)in Java or" ".join(list)in Python. - Template engines — in web development, templates are often used where strings are formed with data substitution.
Example in C# using StringBuilder:
var sb = new StringBuilder();
sb.Append("Hello");
sb.Append(" ");
sb.Append("world");
string result = sb.ToString();