内容简介:Golang语言字符串处理函数包strings,字符串类型转换包strconv。运行结果:
Golang语言字符串处理函数包strings,字符串类型转换包strconv。
/**
* Golang语言字符串处理函数包strings,字符串类型转换包strconv
*/
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
str := "This is an example of a string"
fmt.Printf("Ture/False? Does the string \"%s\" have prefix %s?", str, "Th")
// 判断字符串的前缀
fmt.Printf("%t\n", strings.HasPrefix(str, "Th"))
fmt.Printf("True/False? Does the string \"%s\" have suffix %s?", str, "ing")
// 判断字符串的后缀
fmt.Printf("%t\n", strings.HasSuffix(str, "ing"))
fmt.Printf("True/False? Does the string \"%s\" have %s?", str, "xa")
// 判断字符串是否包含某字符
fmt.Printf("%t\n", strings.Contains(str, "xa"))
// 判断指定字符在字符串第一次出现的位置
fmt.Printf("%d\n", strings.Index(str, "s"))
// 判断指定字符在字符串最后一次出现的位置
fmt.Printf("%d\n", strings.LastIndex(str, "s"))
// 将字符串中的前n个字符替换,并返回一个新字符串,如果n=-1则替换所有字符串中的字符
fmt.Printf("%s\n", strings.Replace(str, "is", "at", 1))
// 统计指定字符在字符串中出现的次数
fmt.Printf("%d\n", strings.Count(str, "s"))
// 将字符串重复n次,并返回新字符串
fmt.Printf("%s\n", strings.Repeat(str, 2))
// 将字符串全部转换为小写字符
fmt.Printf("%s\n", strings.ToLower(str))
// 将字符串全部转换为大写字符
fmt.Printf("%s\n", strings.ToUpper(str))
// 去除字符串开头和结尾的空格
str1 := " This is an example of a string "
fmt.Printf("%s\n", strings.TrimSpace(str1))
// 去除字符串开头和结尾的指定字符,只去除字符串开头或结尾的指定字符
str2 := "my name is frank, this pencil is my"
fmt.Printf("%s\n", strings.Trim(str2, "my"))
fmt.Printf("%s\n", strings.TrimLeft(str2, "my"))
fmt.Printf("%s\n", strings.TrimRight(str2, "my"))
// 分割字符串,转换为一个slice
// 利用1个或多个空白字符来作为分隔符,将字符串分割成若干小块,并返回一个slice
// 如果字符串只包含空白字符串,则返回一个长度为0的slice
sli := strings.Fields(str)
for _, value := range sli {
fmt.Println(value)
}
str3 := "2019-05-07"
sli2 := strings.Split(str3, "-")
for _, value := range sli2 {
fmt.Println(value)
}
// 拼接slice元素成为一个字符串
fmt.Printf("%s\n", strings.Join(sli2, "-"))
// 字符串类型转换
str4 := "123"
// 字符串类型转换为int
num, _ := strconv.Atoi(str4)
fmt.Printf("%d\n", num)
// int转换为字符串类型
newStr4 := strconv.Itoa(num)
fmt.Printf("%s\n", newStr4)
}
运行结果:
Ture/False? Does the string "This is an example of a string" have prefix Th?true True/False? Does the string "This is an example of a string" have suffix ing?true True/False? Does the string "This is an example of a string" have xa?true 3 24 That is an example of a string 3 This is an example of a stringThis is an example of a string this is an example of a string THIS IS AN EXAMPLE OF A STRING This is an example of a string name is frank, this pencil is name is frank, this pencil is my my name is frank, this pencil is This is an example of a string 2019 05 07 2019-05-07 123 123
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 查找一个字符串中最长不含重复字符的子字符串,计算该最长子字符串的长度
- 字符串、字符处理总结
- 高频算法面试题(字符串)leetcode 387. 字符串中的第一个唯一字符
- php删除字符串最后一个字符
- (三)C语言之字符串与字符串函数
- 算法笔记字符串处理问题H:编排字符串(2064)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
人工智能
腾讯研究院、中国信通院互联网法律研究中心、腾讯AI Lab、腾讯开放平台 / 中国人民大学出版社 / 2017-10-25 / 68.00元
面对科技的迅猛发展,中国政府制定了《新一代人工智能发展规划》,将人工智能上升到国家战略层面,并提出:不仅人工智能产业要成为新的经济增长点,而且要在2030年达到世界领先水平,让中国成为世界主要人工智能创新中心,为跻身创新型国家前列和经济强国奠定基础。 《人工智能》一书由腾讯一流团队与工信部高端智库倾力创作。本书从人工智能这一颠覆性技术的前世今生说起,对人工智能产业全貌、最新进展、发展趋势进行......一起来看看 《人工智能》 这本书的介绍吧!