til/Development/Go/strings.md

30 lines
588 B
Markdown
Raw Normal View History

2020-07-10 10:57:16 +00:00
# [strings](https://golang.org/pkg/strings/)
2020-08-29 06:28:01 +00:00
Replace list of strings by using `Replacer`:
2020-07-10 10:57:16 +00:00
```go
template := "My city is {city} and my country is {country}"
r := strings.NewReplacer("{city}", "Sai Gon", "{country}", "Viet Nam")
fmt.Println(r.Replace(template))
```
2020-09-06 09:35:58 +00:00
Use `Builder` to **efficently** concat string:
```go
func oldConcat(strs ...string) string {
var result string
for _, str := range strs {
result += str
}
return result
}
func newConcat(strs ...string) string {
var sb strings.Builder
for _, str := range strs {
sb.WriteString(str)
}
return sb.String()
}
```