Sobes.tech
Junior — Middle

Can you give examples of different string formatting methods in programming?

sobes.tech AI

Answer from AI

In programming, strings can be formed in various ways depending on the language and task:

  • Concatenation — simple joining of strings using the + operator or functions. For example, in Go:
s := "Hello, " + "World!"
  • String formatting — using functions like fmt.Sprintf to insert values into a template:
name := "Alice"
age := 30
s := fmt.Sprintf("Name: %s, Age: %d", name, age)
  • String builder (strings.Builder) — an efficient way to append parts of a string sequentially without creating many temporary objects:
var builder strings.Builder
builder.WriteString("Hello")
builder.WriteString(", ")
builder.WriteString("World!")
s := builder.String()
  • Using byte slices — for low-level string operations, such as reading from streams or network data.

Each method is suitable for different scenarios; for example, strings.Builder is efficient when constructing large strings in loops.

Can you give examples of different string formatting… - sobes.tech