go语言字符串函数小结
拼接字符串
- func Join(a []string, sep string) string, 拼接字符串,将一个[]string的切片通过分隔符,拼接成一个字符串,类似于PHP的implode()
s := []string{"hello", "word", "ED"}
fmt.Println(strings.Join(s, "-")) // hello-word-ED
切割字符串
- func Split(s, sep string) []string, 拆分字符串,将一个string拆分成一个[]string的切片,类似于PHP的explode()
prefixemail := strings.Split("xhstytome@163.com", "@")
fmt.Println(prefixemail) // [xhstytome 163.com]
- func SplitAfter(s, sep string) []string,这个函数是在前边的切割完成之后再后边在加上sep分割符
fmt.Println(strings.SplitAfter("a,b,c,d", ",")) //[a, b, c, d]
- func Fields(s string) []string,这个函数的作用是按照1:n个空格来分割字符串最后返回的是[]string的切片
fmt.Println(strings.Fields("hello widuu golang")) //out [hello widuu golang]
- func FieldsFunc(s string, f func(rune) bool) []string, 一看就了解了,这就是根据自定义函数分割了
func main() {
fmt.Println(strings.FieldsFunc("widuunhellonword", split)) // [widuu hello word]根据n字符分割
}
// 将字符串按照split函数进行分割
func split(s rune) bool {
if s == 'n' {
return true
}
return false
}
- func SplitAfterN(s, sep string, n int) []string该函数s根据sep分割,返回分割之后子字符串的slice,和split一样,只是返回的子字符串保留sep,如果sep为空,那么每一个字符都分割
fmt.Println(strings.SplitAfterN("a,b,c,d,r", ",", 4)) //["a," "b," "c," "d,r"]
fmt.Println(strings.SplitAfterN("a,b,c,d,r", ",", 5)) //["a," "b," "c," "d," "r"]
- func SplitN(s, sep string, n int) []string,这个是切割字符串的时候自己定义长度,如果sep为空,那么每一个字符都分割
fmt.Println(strings.SplitN("a,b,c", ",", 2)) //[a b,c]
正则替换字符串
reg := regexp.MustCompile(`(liz|dan|mua)`)
fmt.Printf("%s\n", reg.ReplaceAllString("liz123 danMFC 8ca. mua!", "**")) //**123 ** MFC 8ca. **! Golang/
修剪字符串:strings包中的部分函数trim
1、func Trim(s string, cutset string) string
将字符串s中首尾包含cutset中的任一字符去掉返回
2、func TrimFunc(s string, f func(rune) bool) string
将字符串s首尾满足函数f(r)==true的字符去掉返回
3、func TrimLeft(s string, cutset string) string
将字符串s左边包含cutset中的任一字符去掉返回
4、func TrimLeftFunc(s string, f func(rune) bool) string
将字符串s左边满足函数f(r)==true的字符去掉返回
5、func TrimRight(s string, cutset string) string
将字符串s右边包含cutset中的任一字符去掉返回
6、func TrimRightFunc(s string, f func(rune) bool) string
将字符串s右边满足函数f(r)==true的字符去掉返回
7、func TrimSpace(s string) string
将字符串s首尾空白去掉返回
8、func TrimPrefix(s, prefix string) string
将字符串s中前缀字符串prefix去掉返回
9、func TrimSuffix(s, suffix string) string
将字符串s中后缀字符串prefix去掉返回
*/
go语言字符串函数小结的更多相关文章
- 13-C语言字符串函数库
目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...
- C语言字符串函数大全
C语言字符串函数大全 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include ...
- php常用字符串函数小结
php内置了98个字符串函数(除了基于正则表达式的函数,正则表达式在此不在讨论范围),能够处理字符串中能遇到的每一个方面内容,本文对常用字符串函数进行简单的小结,主要包含以下8部分:1.确定字符串长度 ...
- C语言-字符串函数的实现(一)之strlen
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- C语言-字符串函数的实现(五)之strstr
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- C语言-字符串函数的实现(二)之strcpy
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- C语言字符串函数例子程序大全 – string相关
关于字符串函数的应用细则,例子程序 – jerny 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source) ...
- 关于C语言字符串函数使用的一点心得
就字符串的拼接函数为例strcat. 原型:extern char *strcat(char *dest,char *src);用法:#include <string.h> 功能:把src ...
- C语言字符串函数
strtok() 字符串分割函数strstr() 字符串查找函数 范例 #include <string.h> main() { char * s = " ...
随机推荐
- Redis CrackIT 入侵事件引发Linux 沦陷
▲针对全球6379端口的redis服务器做了扫描,结果如上图 如图开放在公网的redis的6379端口的ip总数有63443个.无密码认证的IP有43024个,在总数占比里达到67%.发现遭受到red ...
- git_push报错
转自 https://blog.csdn.net/u010042585/article/details/79378726 将本地项目push到GitHub时遇到的问题 .$ git remote ad ...
- Flutter的布局方法
重点是什么? Widgets 是用于构建UI的类. Widgets 用于布局和UI元素. 通过简单的widget来构建复杂的widget Flutter布局机制的核心就是widget.在Flutter ...
- python之selenium多窗口切换
前提: 在页面操作过程中有时候点击某个链接会弹出新的窗口,这就需要主机切换到新打开的窗口上.WebDriver提供了switch_to.window()方法,可以实现在不同的窗口之间切换. 内容: 以 ...
- 6种php加密解密方法
<?php function encryptDecrypt($key, $string, $decrypt){ if($decrypt){ $decrypted = rtrim(mcrypt_d ...
- CentOS 7安装gevent
CentOS 7安装gevent 首先确认python版本不低于python 2.7 执行python –V(或者python -vision) 安装pip工具 方式一: [root@server-0 ...
- Java学习笔记-抽象类与接口
抽象类用于在类中不用具体实现,而在子类中去实现的类 抽象类 抽象类概述 抽象定义:抽象就是从多个事物中将共性的,本质的内容抽取出来 抽象类:Java中可以定义没有方法体的方法,该方法的具体实现由子类完 ...
- 《The C Programming Language》学习笔记
第五章:指针和数组 单目运算符的优先级均为2,且结合方向为自右向左. *ip++; // 将指针ip的值加1,然后获取指针ip所指向的数据的值 (*ip)++; // 将指针ip所指向的数据的值加1 ...
- [转帖]密钥库文件格式(Keystore)和证书文件格式(Certificate)
密钥库文件格式[keystore]代码 https://blog.csdn.net/zzhongcy/article/details/22755317 格式 : JKS 扩展名 : .jks/.ks ...
- 【转贴】linux 终端报Message from syslogd
linux 终端报Message from syslogd xiao9873341760人评论8537人阅读2017-03-27 14:19:31 https://blog.51cto.com/xia ...