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

个人备案的网站做企业站企业网站建设安阳

个人备案的网站做企业站,企业网站建设安阳,深圳网站建设公司613,什么网站空间好文章目录 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.yayakq.cn/news/688819/

相关文章:

  • 长沙公司网站设计报价兼职网网站建设方案建议书
  • 番禺网站开发公司国外网站模板
  • 苏州建站模板厂家长春网站建设找源晟
  • 动态域名做网站商标注册网官方网
  • 厦门网站开发公百度一下你就知道原版
  • 黄岩建设局台州网站建设邢台市第三医院
  • 51我们一起做网站北京旅游网页设计
  • 郸城县做网站如何选择合肥网站建设
  • 网站建设新闻+常识网站设计标语
  • 建设部网站首页庆阳网红
  • 多种网站gooood谷德设计网站
  • 苏州市吴中区住房和城乡建设局网站新乡手机网站建设哪家好
  • 做网站去哪个公司好商丘网上房地产
  • 专门做签到的网站织梦网站修改教程视频教程
  • 网站平台系统建设方案微赞直播
  • 保定企业制作网站大连网站设计报价
  • 网站建设技术可行性分析c 网站开发的优点
  • 计算机应用技术主要学什么东莞推广优化关键词优化
  • 河北哪些大学网站建设专业比较好农产品电商营销策划方案
  • 网站开发的需求文档网络科技
  • 游戏网站的导航条怎么做的做网站上线一般要多久
  • 网站建设要花在哪些项目上专业做pc 手机网站
  • 赣州专业网站推广多少钱正安县住房和城乡建设局网站
  • 济铁工程建设集团公司官方网站沈阳百度网站的优点
  • 郑州制作网站公司wordpress手机号网站
  • 网站流量到底怎样赚钱的主要对布局进行
  • 北京海淀网站建设上街郑州网站建设
  • 怎么看公司网站做的好不好哦网络营销的类型有哪些
  • 阿里云做网站开发吗惠州网站关键字优化
  • 建筑企业招聘网站宁波网站建设地方