拼接字符串

  • 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语言字符串函数小结的更多相关文章

  1. 13-C语言字符串函数库

    目录: 一.C语言字符串函数库 二.用命令行输入参数 回到顶部 一.C语言字符串函数库 1 #include <string.h> 2 字符串复制 strcpy(参数1,参数2); 参数1 ...

  2. C语言字符串函数大全

    C语言字符串函数大全 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include ...

  3. php常用字符串函数小结

    php内置了98个字符串函数(除了基于正则表达式的函数,正则表达式在此不在讨论范围),能够处理字符串中能遇到的每一个方面内容,本文对常用字符串函数进行简单的小结,主要包含以下8部分:1.确定字符串长度 ...

  4. C语言-字符串函数的实现(一)之strlen

    C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...

  5. C语言-字符串函数的实现(五)之strstr

    C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...

  6. C语言-字符串函数的实现(二)之strcpy

    C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...

  7. C语言字符串函数例子程序大全 – string相关

    关于字符串函数的应用细则,例子程序 – jerny 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source) ...

  8. 关于C语言字符串函数使用的一点心得

    就字符串的拼接函数为例strcat. 原型:extern char *strcat(char *dest,char *src);用法:#include <string.h> 功能:把src ...

  9. C语言字符串函数

    strtok()     字符串分割函数strstr()     字符串查找函数 范例 #include <string.h> main() {     char * s = " ...

随机推荐

  1. 【401】Python 求合数的所有质数因子

    对于这样的一个题目来说,出看来,可能会想到判断是否为质数,但其实并不需要. 只要按照从2开始遍历,只要遇到可以整除的就是想要的质数,理由是,如果遇到合数的话,那么在此之前一定会遇到这个合数的质因子,因 ...

  2. python中星号变量的几种特殊用法

    python中星号变量的几种特殊用法 不知道大家知不知道在Python中,星号除了用于乘法数值运算和幂运算外,还有一种特殊的用法"在变量前添加单个星号或两个星号",实现多参数的传入 ...

  3. HashMap和ConcurrentHashMap 源码关键点解析

    第一部分:关键源码讲解 1.HashMap  是如何存储的? a.底层是一个数组 tab b. hash=hash(key) ,然后根据数组长度n和hash值,决定当前需要put的元素对应的数组下标, ...

  4. 在win10上使用premake工具和vs2017编译运行Box2D源码和Testbed

    1.从github上下载Box2D源码的zip包 2.解压缩zip包 3.从premake网站下载premake5工具,解压后得到premake5.exe 4.将premake5.exe拷贝到Box2 ...

  5. Python随笔日记(1)

    Python学习 1.安装python .之后在Windows中配置环境变量(计算机\属性\高级系统设置\环境变量\系统变量\path后加入 :路径) 2.注意变量的命名的规则 字母.数字.下划线 p ...

  6. Java编程思想(三)控制程序流程

    3.1.10逗号运算符 我们可以使用一系列由逗号分隔的语句,而且哪些语句均会独立执行. 3.1.15复习计算顺序

  7. [Agc029A]Irreversible operation_逆序对

    Irreversible operation 题目链接:https://atcoder.jp/contests/agc029/tasks/agc029_a 数据范围:略. 题解: 假设黑色是$1$,白 ...

  8. jQuery实现form表单序列化转换为json对象功能示例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  9. [转帖]sys.dm_exec_connections (Transact-SQL)

    sys.dm_exec_connections (Transact-SQL) https://docs.microsoft.com/en-us/sql/relational-databases/sys ...

  10. [转帖]centos7上设置中文字符集

    centos7上设置中文字符集 https://www.cnblogs.com/kaishirenshi/p/10528034.html author: headsen  chen date: 201 ...