当前位置: 首页 > news >正文

网站收录查询api天津网站建设公司

网站收录查询api,天津网站建设公司,网站编辑的职业特点有哪些,wordpress 加密算法文章目录 string前缀和后缀字符串包含判断子字符串或字符在父字符串中出现的位置字符串替换统计字符串出现次数重复字符串修改字符串大小写修剪字符串分割字符串拼接 slice 到字符串 strconv 本篇主要总结的是go中的string包的一些函数的操作讲解 string 在各个语言中&#x…

文章目录

  • string
    • 前缀和后缀
    • 字符串包含
    • 判断子字符串或字符在父字符串中出现的位置
    • 字符串替换
    • 统计字符串出现次数
    • 重复字符串
    • 修改字符串大小写
    • 修剪字符串
    • 分割字符串
    • 拼接 slice 到字符串
  • strconv

本篇主要总结的是go中的string包的一些函数的操作讲解

string

在各个语言中,都有对应的处理字符串的包,在go中是使用strings来处理的

前缀和后缀

HasPrefix() 判断字符串 s 是否以 prefix 开头:

strings.HasPrefix(s, prefix string) bool

HasSuffix() 判断字符串 s 是否以 suffix 结尾:

strings.HasSuffix(s, suffix string) bool

示例代码

func test1() {fmt.Println(strings.HasPrefix("this is string", "this"))fmt.Println(strings.HasPrefix("this is string", "1this"))fmt.Println(strings.HasSuffix("this is string", "ing"))fmt.Println(strings.HasSuffix("this is string", "iing"))
}

字符串包含

Contains() 判断字符串 s 是否包含 substr:

strings.Contains(s, substr string) bool

示例代码

func test2() {totalString := "hello go, i love cpp"containString1 := "go"containString2 := "cpp"containString3 := "java"fmt.Printf("\"%s\" contain in \"%s\"? the ans is %t\n", containString1, totalString, strings.Contains(totalString, containString1))fmt.Printf("\"%s\" contain in \"%s\"? the ans is %t\n", containString2, totalString, strings.Contains(totalString, containString2))fmt.Printf("\"%s\" contain in \"%s\"? the ans is %t\n", containString3, totalString, strings.Contains(totalString, containString3))
}

这里顺便温习一下对于Go中转义字符的使用

判断子字符串或字符在父字符串中出现的位置

Index() 返回字符串 str 在字符串 s 中的索引(str 的第一个字符的索引),-1 表示字符串 s 不包含字符串 str:

strings.Index(s, str string) int

LastIndex() 返回字符串 str 在字符串 s 中最后出现位置的索引(str 的第一个字符的索引),-1 表示字符串 s 不包含字符串 str:

strings.LastIndex(s, str string) int

示例代码:

func test3() {totalString := "hello go, i love cpp, i love cpp and go"containString1 := "go"containString2 := "cpp"containString3 := "java"fmt.Println("first index demo is:")fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString1, totalString, strings.Index(totalString, containString1))fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString2, totalString, strings.Index(totalString, containString2))fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString3, totalString, strings.Index(totalString, containString3))fmt.Println("last index demo is:")fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString1, totalString, strings.LastIndex(totalString, containString1))fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString2, totalString, strings.LastIndex(totalString, containString2))fmt.Printf("\"%s\" first index in \"%s\" is %d\n", containString3, totalString, strings.LastIndex(totalString, containString3))
}

运行结果为:

first index demo is:
"go" first index in "hello go, i love cpp, i love cpp and go" is 6
"cpp" first index in "hello go, i love cpp, i love cpp and go" is 17
"java" first index in "hello go, i love cpp, i love cpp and go" is -1
last index demo is:
"go" first index in "hello go, i love cpp, i love cpp and go" is 37
"cpp" first index in "hello go, i love cpp, i love cpp and go" is 29
"java" first index in "hello go, i love cpp, i love cpp and go" is -1

如果需要查询非 ASCII 编码的字符在父字符串中的位置,建议使用以下函数来对字符进行定位:

strings.IndexRune(s string, r rune) int

字符串替换

Replace() 用于将字符串 str 中的前 n 个字符串 old 替换为字符串 new,并返回一个新的字符串,如果 n = -1 则替换所有字符串 old 为字符串 new:

strings.Replace(str, old, new string, n int) string

示例代码

func test4() {str1 := "hello hello hello hello hello hello"str2 := strings.Replace(str1, "hello", "no", -1)fmt.Println(str2)
}

这个函数的意思就是只要识别到有可以替换的字符串,并且对于最后一个数字嗯,并没有超过所限制的数量,那么就会将这个识别道德字符串替换为想要替换成的字符串,比如在这个例子当中当识别到字符串中含有哈喽,这个单词是就会将hello替换成no,前提是没有超过-1的限制,而因为-1的意思是,只要有字符串就进行替换,那么就会整个将这个字符串当中所有含有hello的字符串都替换为no

统计字符串出现次数

Count() 用于计算字符串 str 在字符串 s 中出现的非重叠次数:

strings.Count(s, str string) int

示例代码

func test5() {str1 := "hello world hello world hellhello world"fmt.Println(strings.Count(str1, "hello"))fmt.Println(strings.Count(str1, "world"))
}

重复字符串

Repeat() 用于重复 count 次字符串 s 并返回一个新的字符串:

strings.Repeat(s, count int) string

示例代码

func test6() {str := "hello go"fmt.Println(strings.Repeat(str, 10))fmt.Println(strings.Repeat(str, 2))
}

修改字符串大小写

ToLower() 将字符串中的 Unicode 字符全部转换为相应的小写字符:

strings.ToLower(s) string

ToUpper() 将字符串中的 Unicode 字符全部转换为相应的大写字符:

strings.ToUpper(s) string

示例代码

func test7() {str := "hello World This is TEST"fmt.Println(strings.ToLower(str))fmt.Println(strings.ToUpper(str))
}

修剪字符串

你可以使用 strings.TrimSpace(s) 来剔除字符串开头和结尾的空白符号;如果你想要剔除指定字符,则可以使用 strings.Trim(s, “cut”) 来将开头和结尾的 cut 去除掉。该函数的第二个参数可以包含任何字符,如果你只想剔除开头或者结尾的字符串,则可以使用 TrimLeft() 或者 TrimRight() 来实现。

示例代码

func test8() {str1 := "11hello world111"str2 := "  hello go    "fmt.Println("去除空白")fmt.Println(strings.TrimSpace(str1))fmt.Println(strings.TrimSpace(str2))fmt.Println("去除左侧空白")fmt.Println(strings.TrimLeft(str2, " "))fmt.Println("去除左侧字符1")fmt.Println(strings.TrimLeft(str1, "1"))fmt.Println("去除右侧字符1")fmt.Println(strings.TrimRight(str1, "1"))fmt.Println("去除左右两侧1")fmt.Println(strings.Trim(str1, "1"))
}

分割字符串

strings.Fields(s) 将会利用 1 个或多个空白符号来作为动态长度的分隔符将字符串分割成若干小块,并返回一个 slice,如果字符串只包含空白符号,则返回一个长度为 0 的 slice。

strings.Split(s, sep) 用于自定义分割符号来对指定字符串进行分割,同样返回 slice。

因为这 2 个函数都会返回 slice,所以习惯使用 for-range 循环来对其进行处理

示例代码

func test9() {str1 := "hello1 hello2 hello3      hello4"fmt.Println("以一个空格为分隔符")s1 := strings.Split(str1, " ")for _, t := range s1 {fmt.Println(t)}fmt.Println("以一个或多个空格为分隔符")s2 := strings.Fields(str1)for _, t := range s2 {fmt.Println(t)}str3 := "hello11hello22hello321321312hello4"fmt.Println("以hello为分隔符")s3 := strings.Split(str3, "hello")for _, t := range s3 {fmt.Println(t)}
}

拼接 slice 到字符串

Join() 用于将元素类型为 string 的 slice 使用分割符号来拼接组成一个字符串:

strings.Join(sl []string, sep string) string

示例代码

func test10() {// 定义一个字符串切片sl := []string{"apple", "banana", "cherry"}// 使用空格作为分隔符拼接切片中的字符串result := strings.Join(sl, " ")fmt.Println(result) // 输出 "apple banana cherry"// 使用逗号和空格作为分隔符拼接切片中的字符串result2 := strings.Join(sl, ", ")fmt.Println(result2) // 输出 "apple, banana, cherry"
}

strconv

string类型的数据和其他类型的数据进行转换,实际上是借助这个包来完成的

这里只讲述最基本的内容,其他的内容之后再进行讲解

示例代码:

func test11() {str1 := "666"number, _ := strconv.Atoi(str1)fmt.Println(number)fmt.Println(strconv.Itoa(number + 5))
}
http://www.15wanjia.com/news/8208.html

相关文章:

  • wordpress开启防盗链厦门seo
  • 网站后台操作站长推广网
  • 有哪些做鞋机设备的网站长沙网站托管优化
  • wordpress 友情链接插件如何优化培训方式
  • 宿迁建设局网站拆除备案长沙网站搭建关键词排名
  • 网站开发实训要求网络营销师月薪
  • 那个做兼职网站好东莞公司网上推广
  • 做网站购买域名之后信息流优化师需要具备哪些能力
  • 定制类做网站多少钱矿坛器材友情交换
  • 建筑工程合同书范本2023版评论优化
  • jquery win8风格网站图片九宫格排列布局左右全屏图片滚动爱站网工具
  • 广东省建设厅网站网站seo推广平台
  • 网站设计制作售价多少钱aso是什么意思
  • 黄石有没有做网站的今天的新闻
  • 阿里云建站视频教程门户网站有哪些
  • 京伦科技网站做的怎么样商丘seo外包
  • 长春给企业做网站的公司域名查询
  • vps 做网站网站如何优化关键词排名
  • 青岛做网站杭州百度推广电话
  • 石材做网站青岛网络推广公司哪家好
  • 网站建设常用视频格式萧山seo
  • 邢台企业网站制作公司百度分析工具
  • 网站与建设实训报告新东方厨师学费价目表
  • UE做的比较好的网站百度sem是什么
  • .net和php哪个做网站好百度电商平台app
  • 做网站 指导如何让百度收录自己信息
  • 目前最新的营销模式有哪些北京做seo的公司
  • 专业网站制作公司热搜词排行榜
  • 域名备案 没有网站百度宁波运营中心
  • 网站开发框架文档友妙招链接