Go package: strings
Go strings
Go 的 strings 包中包含许多处理字符串的函数
官方文档:https://golang.org/pkg/strings/
前缀、后缀
判断字符串前缀、后缀
// 判断字符串 s 是否以 prefix 开头
func HasPrefix(s, prefix string) bool
// 判断字符串 s 是否以 suffix 结尾
func HasSuffix(s, suffix string) bool
查找子字符串
查找字符串中是否包含子字符串
// 查找字符串 s 中是否包含 substr
func Contains(s, substr string) bool
// 查找字符串 s 中是否包含 chars 中任意字符
func ContainsAny(s, chars string) bool
查找字符串中子字符串的位置
// 查找字符串 s 中第一次出现 substr 的位置,如果不存在返回 -1
func Index(s, substr string) int
// 查找字符串 s 中最后一次出现 substr 的位置,如果不存在返回 -1
func LastIndex(s, substr string) int
// 查找字符串 s 中第一次出现 chars 中任意字符的位置,如果不存在返回 -1
func IndexAny(s, chars string) int
// 查找字符串 s 中最后一次出现 chars 中任意字符的位置,如果不存在返回 -1
func LastIndexAny(s, chars string) int
// 查找字符串 s 中第一次出现 c 字节的位置,如果不存在返回 -1
func IndexByte(s string, c byte) int
修剪
Trim 通过 Unicode 编码是否一致判断去除的片段,cutset 中包含的是要去除的字节,顺序不一致也会被删除
Trim 会连续去除,直至指定位置不存在该片段,但是 TrimPrefix、TrimSuffix 只会去除一次,用于去除前、后缀
// 返回字符串 s 在前后分别去除 cutset 中包含字节的切片
func Trim(s string, cutset string) string
// 返回字符串 s 在前后分别去除空格的切片
// 等于 func Trim(s string, “ ”) string
func TrimSpace(s string) string
// 返回字符串 s 在开头去除 cutset 中包含字节的切片
func TrimLeft(s string, cutset string) string
// 返回字符串 s 在结尾去除 cutset 中包含字节的切片
func TrimRight(s string, cutset string) string
示例:
package main
import (
"fmt"
"strings"
)
func main() {
a := "hello world"
b := strings.TrimLeft(a, "lhed")
c := strings.TrimPrefix(a, "hel")
d := strings.Trim(a, "lhed")
fmt.Println(a)
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
}
输出结果:
hello world
o world
lo world
o wor
分割
将一个字符串以指定分隔符进行分隔,返回一个切片
// 以一个或多个空格为分隔符,对字符串 s 进行分割,空字符串会被舍弃
func Fields(s string) []string
// 以一个字符串为分隔符,对字符串 s 进行分割
// 如果分隔符为空,则将每个字符进行切割
func Split(s, sep string) []string
// 在一个字符串 sep 之后对字符串 s 进行分割
// 如果字符串 sep 为空,则对每个字符进行切割
func SplitAfter(s, sep string) []string
// 以一个字符串为分隔符,从第一个分隔符开始,将字符串 s 最多分割成 n 部分
// 当 n == 0 时,返回 nil;当 n == -1 时,相当于 `Split`
func SplitN(s, sep string, n int) []string
// 和 `SplitAfter` 的区别相当于 `Split` 和 `SplitN`
func SplitAfterN(s, sep string, n int) []string
示例:
package main
import (
"fmt"
"strings"
)
func main() {
a := " hello, world, !"
b := strings.Fields(a)
c := strings.Split(a, " ")
d := strings.Split(a, ",")
e := strings.SplitAfter(a, ",")
f := strings.SplitAfterN(a, ",", 2)
fmt.Println(b, len(b))
fmt.Println(c, len(c))
fmt.Println(d, len(d))
fmt.Println(e, len(e))
fmt.Println(f, len(f))
}
输出结果:
[hello, world, !] 3
[ hello, world, !] 5
[ hello world !] 3
[ hello, world, !] 3
[ hello, world, !] 2
替换
替换会搜素字符串中所有符合条件的子字符串,之后将其替换为新字符串
// 将字符串 s 中包含的 old 字符串替换为 new 字符串,从最左边开始一共替换 n 次
// 如果 old 为空,则从字符串开头开始,字符串开头结尾和每个字符之间添加一个 new,一共添加 n 个
// 如果 n < 0 则替换次数没有限制,相当于 `ReplaceAll`
func Replace(s, old, new string, n int) string
// 和 `Replace` 相似,没有限制
func ReplaceAll(s, old, new string) string
示例:
package main
import (
"fmt"
"strings"
)
func main() {
a := " hello world "
b := strings.Replace(a, " ", ",", 1)
c := strings.Replace(a, "", ",", 3)
d := strings.Replace(a, " ", ",", -1)
e := strings.ReplaceAll(a, " ", ",")
fmt.Println(b)
fmt.Println(c)
fmt.Println(d)
fmt.Println(e)
}
输出结果:
,hello world
, ,h,ello world
,hello,world,
,hello,world,
计数
// 字符串 s 包含子字符串 substr 的数量
// 如果 substr 为空,则返回字符串长度 + 1(字符间隙的数量)
func Count(s, substr string) int
重复
// 将字符串 s 重复 count 次,拼接成一个字符串
// count < 0 时会 panic
func Repeat(s string, count int) string
大小写转换
// 将字符串内所有字母转换成小写
func ToLower(s string) string
// 将字符串中所有字母转换成大写
func ToUpper(s string) string
// 每个单词的第一个字母转换为标题大小写
func Title(s string) string
// 将字符串内所有字母转换为标题大小写
func ToTitle(s string) string
拼接
拼接是切割的反操作
// 以 sep 为分隔符,将 a 中元素拼接起来
func Join(a []string, sep string) string
Go package: strings的更多相关文章
- golang程序编译时提示“package runtime: unrecognized import path "runtime" (import path does not begin with hostname)”
在编译golang的工程时提示错误的, 提示的错误信息如下: package bytes: unrecognized import path "bytes" (import pat ...
- JAVA中的字符串小结
String字符串是只读的,不可变的 查看String类的源码,可以发现String类是被final关键字修饰的: 另外还可以看下String类源码中的其它方法实现,随便举个可以修改String值的方 ...
- The Go Programming Language. Notes.
Contents Tutorial Hello, World Command-Line Arguments Finding Duplicate Lines A Web Server Loose End ...
- Java的String和StringBuilder
一.String 1.创建String对象的方法: String s1="haha"; String s2=new String(); String s3=new String(& ...
- go语言标准库 时刻更新
Packages Standard library Other packages Sub-repositories Community Standard library ▾ Name Synops ...
- java 扫描输入
到目前为止,从文件或标准输入读取数据还是一件相当痛苦第事情,一般第解决之道就是读入一行文本,对其进行分词,然后使用Integer Double 等类第各种解析方法来解析数据: //: strings/ ...
- java 格式化
一. 可以之际像c语言一样用System.out.printf()格式化输出 二. System.out.format 1. format()方法模仿自printf(), 可用于PrintStream ...
- java jvm 字节码 实例
https://blog.csdn.net/wuzhiwei549/article/details/80626677 代码 package strings; //: strings/WhitherSt ...
- 08 Packages 包
Packages Standard library Other packages Sub-repositories Community Standard library Name Synopsis ...
随机推荐
- DotNet Core中使用dapper
我们都知道,ORM全称是,Object Relationship Mapper,即,对象关系映射.也就是可以用object来map我们的db,而且市面上的orm框架有很多,其中有一个框架叫做dappe ...
- Python3如何安装pip工具?
前几天安装Python的时候没有装上pip工具,所以只能现在手动安装了. 首先,访问https://bootstrap.pypa.io/get-pip.py这个网址,然后Ctrl+S将get-pip. ...
- ElementUi中el-table分页效果
现实的场景中很经常遇到表格el-table数据过多,为了更好的用户体验,所以我们需要用到分页,一般分页可以视数据量的大小可分为前端控制和后端控制. 先看下效果(已做脱敏处理) 图1 前端el-tabl ...
- Mybatis XML映射文件
mybatis为聚焦于SQL而构建,SQL映射文件常用的顶级元素如 resultMap,是最复杂也是最强大的元素,用来描述如何从数据库结果集中来加载对象. insert,映射插入语句 update, ...
- sql server使用sp_executesql返回拼接字符串里面的输出参数
问题: 今天一同事请教博主,他拼接了一个语句,select表格形式数据,然后使用@@rowcount获取到行数. 但他又有这样特别的需求:想只获取行数而不返回表格数据结果,因为是while循环,不想返 ...
- FTP 代码含义
vsftpd.config 部分参数含义anonymous_enable=NO #不允许匿名用户登陆 local_enable=YES #vsftpd所在系统的用户可以登录vsftpd write_e ...
- (办公)记事本_Linux目录
转载自菜鸟教程:https://www.runoob.com/linux/linux-system-contents.html /bin: bin是Binary的缩写, 这个目录存放着最经常使用的命令 ...
- SSM案例整合踩的一些坑
一.出现错误:Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for p ...
- Redis的List的删除
Redis的List命令里没有根据index删除元素的命令,但有的时候业务会需要这个功能. 先上命令: LSET ListKey index "__deleted__"LREM L ...
- [译]Vulkan教程(29)组合的Image采样器
[译]Vulkan教程(29)组合的Image采样器 Combined image sampler 组合的image采样器 Introduction 入门 We looked at descripto ...