til/Development/Go/strings.md

36 lines
708 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()
}
```
2020-10-28 04:34:56 +00:00
Use `EqualFold` for faster compare strings with upper, lower or mixed case:
```go
strings.EqualFold("Foo", "fOO")
```