Go常用包系列---strings包
来源:互联网 发布:手机pdf免费编辑软件 编辑:程序博客网 时间:2024/05/16 12:53
strings包
函数
说明
strings.HasPrefix(s, prefix string) bool
判断字符串s是否以prefix 开头
strings.HasSuffix(s, suffix string) bool
判断字符串是否以suffix结尾
strings.Contains(s, substr string) bool
判断字符串s是否包含substr
strings.Index(s, str string) int
判断子字符串在父字符串中第一次出现的位置
strings.LastIndex(s, str string) int
判断子字符串在父字符串中最后出现的位置
strings.IndexRune(s, r rune) int
判断ASCII编码的字符在父字符串中最后出现的位置
strings.Count(s, str string) int
计算字符串str在字符串s中出现的非重叠的次数strings.Repeat(s, count int) string
重复count次字符串s并返回一个新的字符串
strings.ToLower(s) string
将字符串中的Unicode字符全部转换为相应小写字符
strings.ToUpper(s) string
将字符串中的Unicode字符全部转换为相应大写字符
strings.TrimSpace(s string) string
去除字符串两侧的空白字符strings.Trim(s string, cutset string) string
去掉字符串两侧的cutset字符串中的任意字符 *
strings.TrimLeft(s string, cutset string) string
去掉字符串左侧的cutset字符串中的任意字符
strings.TrimRight(s string, cutset string) string
去掉字符串右侧的cutset字符串中的任意字符
strings.TrimPrefix(s string, cutset string) string
去掉字符串开头的cutset字符串
strings.TrimSuffix(s string, cutset string) string
去掉字符串结尾的cutset字符串
strings.Fields(s string) []string
以1个或多个空白字符分隔字符串s并返回slicestrings.Split(s string, sep string) []string
以sep分隔字符串s并返回slice
strings.Join(a []string, sep string) string
将元素类型为string的slice使用sep拼接返回一个字符串例:
package main
import (
"fmt"
"strings"
)
func main() {
/* strings 包使用 */
str := " This is an example of a string "
prefix := "T"
if strings.HasPrefix(str, prefix) {
fmt.Printf("the string '%s' have prefix %s\n", str, prefix)
}
suffix := "g"
if strings.HasSuffix(str, suffix) {
fmt.Printf("the string '%s' have suffix %s\n", str, suffix)
}
substr := "example"
if strings.Contains(str, substr) {
fmt.Printf("the string '%s' contains '%s'\n", str, substr)
}
//Index返回字符串substr在字符串str中的第一个索引(-1表示字符串str不包含字符串substr),索引从0开始
pos := strings.Index(str, substr)
fmt.Printf("字符串 '%s' 在字符串 '%s' 中的位置是%d\n", substr, str, pos)
//Index返回字符串substr在字符串str中最后出现位置的索引(-1表示字符串str不包含字符串substr),索引从0开始
lastPos := strings.LastIndex(str, "a")
fmt.Printf("字符串 'a' 在字符串 '%s' 中最后出现的位置是%d\n", str, lastPos)
runePos := strings.IndexRune(str, 'a')
fmt.Printf("字符'a' 在字符串 '%s' 中出现的位置是%d\n", str, runePos)
count := strings.Count(str, "a")
fmt.Printf("the string 'a' has appeared %d times in '%s'\n", count, str)
repeatStr := "repeat string!"
repeatStr = strings.Repeat(repeatStr, 2)
fmt.Printf("重复后的字符串为'%s'\n", repeatStr)
upperStr := strings.ToUpper(str)
fmt.Printf("字符串大写:'%s'\n", upperStr)
lowerStr := strings.ToLower(upperStr)
fmt.Printf("字符串小写:'%s'\n", lowerStr)
newStr := strings.TrimSpace(str)
fmt.Printf("原始字符串为:'%s'\n", str)
fmt.Printf("去掉两边的空格后字符串为:'%s'\n", newStr)
cutset1 := "Ths"
s1 := "ThisisGThiss"
newStr1 := strings.Trim(s1, cutset1)
fmt.Printf("'%s'去掉两边的'%s'后字符串为:'%s'\n", s1, cutset1, newStr1)
//注意与strings.TrimPrefix的区别
s2 := "ThisisGThiss"
cutset2 := "This"
newStr2 := strings.TrimLeft(s2, cutset2)
fmt.Printf("'%s'去掉左边的'%s'后字符串为:'%s'\n", s2, cutset2, newStr2)
s4 := "ThisisGisThiss"
cutset4 := "This"
newStr4 := strings.TrimPrefix(s4, cutset4)
fmt.Printf("去掉'%s'的前缀'%s'后的字符串为:'%s'\n", s4, cutset4, newStr4)
//注意与strings.TrimSuffix的区别
newStr3 := strings.TrimRight(s2, cutset2)
fmt.Printf("'%s'去掉右边的'%s'后字符串为:'%s'\n", s2, cutset2, newStr3)
s5 := "ThisisGisThis"
cutset5 := "This"
newStr5 := strings.TrimSuffix(s5, cutset5)
fmt.Printf("去掉'%s'的后缀'%s'后的字符串为:'%s'\n", s5, cutset5, newStr5)
s6 := "sss,s s"
fmt.Printf("Fields are: %q\n", strings.Fields(s6))
fmt.Printf("Fields are: %q\n", strings.Split(s6, ","))
slice1 := strings.Split(s6, ",")
fmt.Printf("The string is: %q\n", strings.Join(slice1, "#"))
}
0 0
- Go常用包系列---strings包
- Go常用包系列---time包
- Go语言——strings包常用函数整理
- Go常用包
- Go 常用包介绍
- go语言 看代码,学strings包
- Go 语言标准库之 strings包
- Go语言学习之strings包(the way to go)
- go常用引用包推荐
- 【Go】包
- TeX系列: 常用宏包
- golang strings包
- golang strings包方法
- Go语言_HTTP包
- [go语言]包
- Go path包
- Go user包
- Go闭包
- 各种格式的时间日期正则表达式
- 基于DSS的先侦听后推送式流媒体转发
- Codeforces round #298
- java通过串口读取数据
- linux下的ssh配置
- Go常用包系列---strings包
- shell 简单oo编程
- Netty系列之Netty线程模型
- 开源性能测试工具Jmeter
- Xamarin.Android Adapter初探
- 神器 Brython —— 将 python 当做客户端脚本使用
- bss,data,text,rodata,堆,栈,常量段
- 详解C#中的反射
- 2015年第六周项目三:人数不定的工资类