strings.TrimSpace() 去除字符串两边空格
案例:
package main
import(
"fmt"
"strings"
)
func main() {
str := " 你好,我是李萌萌 "
fmt.Printf("输出结果是:%v\n", strings.TrimSpace(str))//输出结果是:你好,我是李萌萌
}
strings.Trim() 将字符串两边指定的字符去除
package main
import(
"fmt"
"strings"
)
func main() {
str := "你好我是李萌萌你好"
fmt.Printf("输出结果是:%v\n", strings.Trim(str, "你好"))//输出的结果是:我是李萌萌
}
strings.TrimLeft() 将字符串左边的指定字符去除
案例:
package main
import(
"fmt"
"strings"
)
func main() {
str := "你好我是李萌萌"
fmt.Printf("输出的结果是:%v\n", strings.TrimLeft(str, "你好"))//输出的结果是:我是李萌萌
}
strings. TrimRight () 将字符串右边指定字符去除
案例:
package main
import(
"fmt"
"strings"
)
func main() {
str := "你好我是李萌萌"
fmt.Printf("输出结果是:%v\n", strings.TrimRight(str, "我是李萌萌"))//输出的结果是:你好
}
strings.HasPrefix() 判断字符串是否以指定字符开头
案例:
package main
import (
"fmt"
"strings"
)
func main() {
str := "hello,我是李萌萌"
fmt.Printf("输出的结果是:%v\n", strings.HasPrefix(str, "hello"))//输出的结果是:true
}
strings.HasSuffix() 判断字符串是否以指定字符结尾
案例:
package main
import(
"fmt"
"strings"
)
func main() {
str := "我是李萌萌"
fmt.Printf("输出的结果是:%v\n", strings.HasSuffix(str, "赵晗雪"))//输出的结果是:false
}