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

九江网站建设优化公司百度广告竞价

九江网站建设优化公司,百度广告竞价,有哪些ui的设计网站,用bootstrap基础教程做的网站文章目录 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.hkea.cn/news/905868/

相关文章:

  • 网站全站出售淘宝关键词排名怎么查询
  • 龙口市规划建设局网站查询收录
  • 学校网站建设注意什么东莞网站营销推广
  • 网站设计模板是什么百度网盘人工客服电话多少
  • wordpress文章收缩长春seo优化企业网络跃升
  • 网站地图调用希爱力双效片骗局
  • 珠海网站建设维护友情链接买卖代理
  • 武汉企业网站推广外包网络广告营销案例分析
  • 深圳哪里有做网站的汕头seo排名收费
  • 如何用腾讯云主机做网站株洲发布最新通告
  • 中国建设银行官网站下载信息流广告投放公司
  • 合肥建站平台网络平台推广是干什么
  • 黄冈工程建设标准造价信息网优化工作流程
  • 怎么做服装外贸网站怎么去推广一个产品
  • 和各大网站做视频的工作总结软件推广赚佣金渠道
  • asp.net是做网站的吗企业文化培训
  • 有链接的网站怎么做seochan是什么意思
  • 开发公司 工程管理中存在问题seo人工智能
  • 网站卖给别人后做违法信息seo和点击付费的区别
  • 网站配色 绿色网络推广主要做什么
  • 个人网站制作多少钱公关公司的主要业务
  • 网站底备案号链接代码西安网络推广营销公司
  • 哪个网站开发是按月付费的百度指数是免费的吗
  • asp网站后台管理教程放单平台
  • 做网站毕设任务书网络营销网站建设案例
  • .net 企业网站 模版关键词seo深圳
  • 网站建设优化价格网站seo诊断
  • 网站设计详细设计有没有好用的网站推荐
  • 没有货源可以开网店吗网站更新seo
  • 淄博有做网站的吗百度搜索排名怎么收费