备忘录吕吕没有备忘录十新建
检查字符串是否以子字符串开头 (Check if a string starts with a substring)
package main
import (
"strings"
)
func main() {
strings.HasPrefix("flavio", "fla") // true
}
检查字符串是否以子字符串结尾 (Check if a string ends with a substring)
package main
import (
"strings"
)
func main() {
strings.HasSuffix("flavio", "vio") // true
}
计算字符串切片中的最大字符串长度 (Calculate the maximum string length in a slice of strings)
// calculatemaxwidth given a slice of strings calculates the maximum
// length
func calculatemaxwidth(lines []string) int {
w := 0
for _, l := range lines {
len := utf8.RuneCountInString(l)
if len > w {
w = len
}
}
return w
}
比较不区分大小写的字符串 (Comparing strings case insensitive)
Instead of running ToUpper()
or ToLower()
from the strings
or bytes
packages, use strings.EqualFold()
or bytes.EqualFold()
, because they are guaranteed to work across all languages.
不要从strings
或bytes
包中运行ToUpper()
或ToLower()
,而是使用strings.EqualFold()
或bytes.EqualFold()
,因为可以保证它们可以在所有语言中使用。
package main
import (
"bytes"
"fmt"
)
func main() {
fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))
}
备忘录吕吕没有备忘录十新建