Go基础编程:字符串处理
本文转自:https://github.com/astaxie/build-web-application-with-golang/blob/master/zh/07.6.md
字符串在开发中经常用到,包括用户的输入,数据库读取的数据等,我们经常需要对字符串进行分割、连接、转换等操作,我们可以通过Go标准库中的strings和strconv两个包中的函数进行相应的操作。
1 字符串操作
下面这些函数来自于strings包,这里介绍一些我平常经常用到的函数,更详细的请参考官方的文档。
1.1 Contains
func Contains(s, substr string) bool
功能:字符串s中是否包含substr,返回bool值
示例代码:
fmt.Println(strings.Contains("seafood", "foo"))fmt.Println(strings.Contains("seafood", "bar"))fmt.Println(strings.Contains("seafood", ""))fmt.Println(strings.Contains("", ""))//运行结果://true//false//true//true
1.2 Join
func Join(a []string, sep string) string
功能:字符串链接,把slice a通过sep链接起来
示例代码:
s := []string{"foo", "bar", "baz"}fmt.Println(strings.Join(s, ", "))//运行结果:foo, bar, baz
1.3 Index
func Index(s, sep string) int
功能:在字符串s中查找sep所在的位置,返回位置值,找不到返回-1
示例代码:
fmt.Println(strings.Index("chicken", "ken"))fmt.Println(strings.Index("chicken", "dmr"))//运行结果:// 4// -1
1.4 Repeat
func Repeat(s string, count int) string
功能:重复s字符串count次,最后返回重复的字符串
示例代码:
fmt.Println("ba" + strings.Repeat("na", 2))//运行结果:banana
1.5 Replace
func Replace(s, old, new string, n int) string
功能:在s字符串中,把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换
示例代码:
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))//运行结果://oinky oinky oink//moo moo moo
1.6 Split
func Split(s, sep string) []string
功能:把s字符串按照sep分割,返回slice
示例代码:
fmt.Printf("%q\n", strings.Split("a,b,c", ","))fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))fmt.Printf("%q\n", strings.Split(" xyz ", ""))fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))//运行结果://["a" "b" "c"]//["" "man " "plan " "canal panama"]//[" " "x" "y" "z" " "]//[""]
1.7 Trim
func Trim(s string, cutset string) string
功能:在s字符串的头部和尾部去除cutset指定的字符串
示例代码:
fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))//运行结果:["Achtung"]
1.8 Fields
func Fields(s string) []string
功能:去除s字符串的空格符,并且按照空格分割返回slice
示例代码:
fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))//运行结果:Fields are: ["foo" "bar" "baz"]
2 字符串转换
字符串转化的函数在strconv中,如下也只是列出一些常用的。
2.1 Append
Append 系列函数将整数等转换为字符串后,添加到现有的字节数组中。
示例代码:
str := make([]byte, 0, 100)str = strconv.AppendInt(str, 4567, 10) //以10进制方式追加str = strconv.AppendBool(str, false)str = strconv.AppendQuote(str, "abcdefg")str = strconv.AppendQuoteRune(str, '单')fmt.Println(string(str)) //4567false"abcdefg"'单'
2.2 Format
Format 系列函数把其他类型的转换为字符串。
示例代码:
a := strconv.FormatBool(false)b := strconv.FormatInt(1234, 10)c := strconv.FormatUint(12345, 10)d := strconv.Itoa(1023)fmt.Println(a, b, c, d) //false 1234 12345 1023
2.3 Parse
Parse 系列函数把字符串转换为其他类型。
示例代码:
package mainimport ("fmt""strconv"
)func checkError(e error) {if e != nil {fmt.Println(e)}
}
func main() {a, err := strconv.ParseBool("false")checkError(err)b, err := strconv.ParseFloat("123.23", 64)checkError(err)c, err := strconv.ParseInt("1234", 10, 64)checkError(err)d, err := strconv.ParseUint("12345", 10, 64)checkError(err)e, err := strconv.Atoi("1023")checkError(err)fmt.Println(a, b, c, d, e) //false 123.23 1234 12345 1023
}
Go基础编程:字符串处理相关推荐
- 1.7-06编程基础之字符串 字符翻转
1.7编程基础之字符串 06:合法 C 标识符 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个不包含空白符的字符串,请判断是否是C语言合法的标识符号(注:题目保证这些字符串一定 ...
- 1.7 编程基础之字符串 34 回文子串 python
http://noi.openjudge.cn/ch0107/34/ """1.7 编程基础之字符串 34 回文子串 http://noi.openjudge.cn/ch ...
- 1.7 编程基础之字符串 30 字符环 python
http://noi.openjudge.cn/ch0107/30/ """ 1.7 编程基础之字符串 30 字符环 http://noi.openjudge.cn/ch ...
- 1.7 编程基础之字符串 31 字符串p型编码 python
http://noi.openjudge.cn/ch0107/31/ """ 1.7 编程基础之字符串 31 字符串p型编码 http://noi.openjudge.c ...
- 1.7 编程基础之字符串 32 行程长度编码 python
http://noi.openjudge.cn/ch0107/32/ """ 1.7 编程基础之字符串 32 行程长度编码 http://noi.openjudge.cn ...
- 1.7 编程基础之字符串 33 判断字符串是否为回文 python
http:// http://noi.openjudge.cn/ch0107/33/ """1.7 编程基础之字符串 33 判断字符串是否为回文http://noi.op ...
- 1.7 编程基础之字符串 27 单词翻转 4分 python
""" 1.7 编程基础之字符串 27 单词翻转 4分 http://noi.openjudge.cn/ch0107/24/ https://blog.csdn.net/ ...
- 1.7 编程基础之字符串 25 最长最短单词 python
""" 1.7 编程基础之字符串 25 最长最短单词 http://noi.openjudge.cn/ch0107/25/ https://blog.csdn.net/h ...
- 1.7 编程基础之字符串 16 忽略大小写的字符串比较 python
http://noi.openjudge.cn/ch0107/16/ """1.7 编程基础之字符串 16 忽略大小写的字符串比较 http://noi.openjudg ...
- 1.7 编程基础之字符串 14 大小写字母互换 python
http://noi.openjudge.cn/ch0107/14/ """1.7 编程基础之字符串 14 大小写字母互换 AC http://noi.openjudge ...
最新文章
- CentOS报错:TNS-12541: TNS:no listener TNS-12560: TNS:protocol adapter error TNS-00511: No listener
- CCF201612-1 中间数(100分)
- Leetcode 912.排序算法(快排)
- MyBatis中Mapper代理方式
- 小练习-----银行提款机系统
- 我大学时代的好朋友要结婚了!
- 字节跳动教育业务怎么样_字节跳动将重点关注教育业务,今年预计招聘超过一万人...
- 用R语言实现对不平衡数据的四种处理方法
- VC2008编译 配置 PortAudio
- idea json转对象(Java实体类)
- 电脑麦克风,详细教你电脑麦克风没声音怎么设置
- oracle 联合查询去重,oracle两张表关联查询
- 自动驾驶芯片之——FPGA和ASIC介绍
- 开发者投稿—百度大脑新品体验之肤色检测
- Excel 2010 SQL应用098 聚合函数之计算某列空值的数目
- 【航天信息开票软件V3.0金税盘版安装恢复过程】有坑有心得
- 千纸鹤(小纸片)全套源码
- 4【Android 12】InputDispatcher分发事件
- HDU 6194 后缀数组+单调栈
- Cisco CCNA系列课程学习