您的位置:首页 > 文旅 > 美景 > 魅力网络营销公司_制作h5页面的软件_做网络营销推广的公司_培训seo去哪家机构最好

魅力网络营销公司_制作h5页面的软件_做网络营销推广的公司_培训seo去哪家机构最好

2025/5/1 17:51:57 来源:https://blog.csdn.net/demonlg0112/article/details/146610224  浏览:    关键词:魅力网络营销公司_制作h5页面的软件_做网络营销推广的公司_培训seo去哪家机构最好
魅力网络营销公司_制作h5页面的软件_做网络营销推广的公司_培训seo去哪家机构最好

以下是 Go 语言标准库中 stringsstrconv 包的常用方法说明及示例:


一、strings 包(字符串操作)

1. Contains

判断字符串是否包含子串

fmt.Println(strings.Contains("hello world", "ell")) // true
2. Count

统计子串出现次数

fmt.Println(strings.Count("cheese", "e")) // 3
3. Join

字符串拼接

s := []string{"a", "b", "c"}
fmt.Println(strings.Join(s, "-")) // a-b-c
4. Split

字符串分割

fmt.Println(strings.Split("a,b,c", ",")) // [a b c]
5. Replace

字符串替换

fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2)) 
// oinky oinky oink
6. Trim

去除首尾指定字符

fmt.Println(strings.Trim("¡¡Hello!¡", "!¡")) // Hello
7. ToUpper/ToLower

大小写转换

fmt.Println(strings.ToUpper("Test")) // TEST
fmt.Println(strings.ToLower("TEST")) // test
8. Index

查找子串位置

fmt.Println(strings.Index("chicken", "ken")) // 4
9. HasPrefix/HasSuffix

前缀/后缀判断

fmt.Println(strings.HasPrefix("Gopher", "Go")) // true
fmt.Println(strings.HasSuffix("Amigo", "go"))  // true
10. Fields

按空白分割字符串

fmt.Println(strings.Fields("  foo bar  baz   ")) 
// [foo bar baz]

二、strconv 包(字符串转换)

1. Atoi

字符串转整型

i, _ := strconv.Atoi("42")
fmt.Println(i) // 42_, err := strconv.Atoi("abc")
fmt.Println(err) // strconv.Atoi: parsing "abc": invalid syntax
2. Itoa

整型转字符串

s := strconv.Itoa(42)
fmt.Println(s) // "42"
3. ParseBool

字符串转布尔值

b, _ := strconv.ParseBool("true")
fmt.Println(b) // true
4. ParseFloat

字符串转浮点数

f, _ := strconv.ParseFloat("3.1415", 64)
fmt.Println(f) // 3.1415
5. FormatFloat

浮点数转字符串

s := strconv.FormatFloat(3.1415, 'f', 2, 64)
fmt.Println(s) // "3.14"
6. ParseInt

指定进制字符串转整型

i, _ := strconv.ParseInt("1010", 2, 64)
fmt.Println(i) // 10 (二进制1010转十进制)
7. FormatInt

整型转指定进制字符串

s := strconv.FormatInt(255, 16)
fmt.Println(s) // "ff"
8. Quote

添加双引号(含特殊字符转义)

fmt.Println(strconv.Quote(`"Hello \n World"`))
// "\"Hello \\n World\""
9. Append系列

高效拼接字符串

buf := []byte("int: ")
buf = strconv.AppendInt(buf, 123, 10)
fmt.Println(string(buf)) // int: 123
10. IsPrint

判断是否可打印字符

fmt.Println(strconv.IsPrint('\u0000')) // false
fmt.Println(strconv.IsPrint('a'))       // true

三、典型使用场景示例

1. 字符串处理综合示例
input := "  Hello, 世界!  "
processed := strings.TrimSpace(input)
parts := strings.Split(processed, " ")
fmt.Println(strings.Join(parts, "-")) 
// Hello,-世界!
2. 数字转换综合示例
// 读取环境变量转为整型
env := "PORT"
val := os.Getenv(env)
port, err := strconv.Atoi(val)
if err != nil {log.Fatalf("Invalid %s: %v", env, err)
}
fmt.Printf("Server starting on port %d", port)
3. 高效字符串拼接
var buf []byte
buf = strconv.AppendBool(buf, true)
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, 2023, 10)
fmt.Println(string(buf)) // true 2023

四、重要特性对比表

功能strings 包strconv 包
字符串包含判断Contains/ContainsAny-
类型转换-Atoi/Itoa/ParseBool 等
字符编码处理ToUpper/ToLower/ToTitleQuote/Unquote
数字格式化-FormatInt/FormatFloat
高效拼接JoinAppendXXX 系列
空白处理TrimSpace/Fields-
进制转换-ParseInt/FormatInt

这些方法覆盖了字符串操作和类型转换的常见需求,建议结合官方文档使用(可通过 go doc strconv.ParseInt 等方式查看详细说明)。实际开发中需要注意错误处理,特别是 AtoiParseBool 等方法需要处理可能的转换错误。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com