当前位置:网站首页>Go language foundation ----- 10 ----- string related operations (operation function, string conversion)
Go language foundation ----- 10 ----- string related operations (operation function, string conversion)
2022-07-03 07:37:00 【Mango sauce】
1 Common functions for string operations
1、Contains
func Contains(s, substr string) bool
function : character string s Include in substr, return bool value
2、Join
func Join(a []string, sep string) string
function : String Links , hold slice a adopt sep link
3、Index
func Index(s, sep string) int
function : In string s Search for sep Where it is , Return position value , No return found -1
4、Repeat
func Repeat(s string, count int) string
function : repeat s character string count Time , Finally return the duplicate string
5、Replace
func Replace(s, old, new string, n int) string
function : stay s In a string , hold old Replace the string with new character string ,n Indicates the number of replacements , Less than 0 Means replace all
6、Split
func Split(s, sep string) []string
function : hold s String according to sep Division , return slice
7、Trim
func Trim(s string, cutset string) string
function : stay s The head and tail of the string are removed cutset Specified string
8、Fields
func Fields(s string) []string
function : Remove s The space character of a string , And return according to the space division slice
2 Code practice
package main
import (
"fmt"
"strings"
)
func main(){
// 1. Contains
//"hellogo" Include in "hello", Include return true, Does not include return false
fmt.Println(strings.Contains("hellogo", "hello"))
fmt.Println(strings.Contains("hellogo", "abc"))
// 2. Joins Combine
s := []string{
"abc", "hello", "mike", "go"}
buf := strings.Join(s, "x")
fmt.Println("buf = ", buf)
// 3. Index, Find the location of the substring
fmt.Println(strings.Index("abcdhello", "hello"))
fmt.Println(strings.Index("abcdhello", "go")) // Returns... Without substring -1
// 4. How many repetitions , Repeat here 3 Time
buf = strings.Repeat("go", 3)
fmt.Println("buf = ", buf) //"gogogo"
// 5. Replace
// stay s In a string , hold old Replace the string with new character string ,n Indicates the number of replacements , Less than 0 Means replace all
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
// 6. Split Split... With the specified separator , Back to the slice
buf = "[email protected]@[email protected]"
s2 := strings.Split(buf, "@")
fmt.Println("s2 = ", s2)
// 7. Trim Remove the characters at both ends
buf = strings.Trim(" are u ok? ", " ") // Remove the space at the end of the head
fmt.Printf("buf = #%s#\n", buf)
fmt.Printf("[%q]\n", strings.Trim(" !!! Achtung !!! ", "! ")) // Output : ["Achtung"]
fmt.Printf("[%q]\n", strings.Trim(" !!! Achtung !!! ", " !")) // The same output : ["Achtung"]
// explain Trim You can remove the space at the beginning and end 、! The content of .
// 8. Remove s The space character of a string , And return according to the space division slice
s3 := strings.Fields(" are u ok? ")
//fmt.Println("s3 = ", s3)
for i, data := range s3 {
fmt.Println(i, ", ", data)
}
}

3 String conversion
package main
import (
"fmt"
"strconv"
)
func main(){
// 1. Convert to string and append to byte array
slice := make([]byte, 0, 1024)
fmt.Println("slice = ", slice)
slice = strconv.AppendBool(slice, true)
// The second number is the number to be appended , The first 3 Specified 10 Add base mode
slice = strconv.AppendInt(slice, 1234, 10)
slice = strconv.AppendQuote(slice, "abcgohello")
fmt.Println("slice = ", string(slice)) // transformation string Then print
fmt.Println("=============================")
// 2. Other types are converted to strings
var str string
str = strconv.FormatBool(false)
fmt.Println("str = ", str)
//'f' Refers to the print format , In decimal form , -1 Refers to the number of decimal places ( Austerity mode ), 64 With float64 Handle
str = strconv.FormatFloat(3.14, 'f', -1, 64)
fmt.Println("str = ", str)
str = strconv.Itoa(6666)// Integer to string , Commonly used
fmt.Println("str = ", str)
fmt.Println("=============================")
// 3. String to other types
var flag bool
var err error
flag, err = strconv.ParseBool("true")
if err == nil {
fmt.Println("flag = ", flag)
} else {
fmt.Println("err = ", err)
}
a, _ := strconv.Atoi("567") // Convert a string to an integer
fmt.Println("a = ", a)
}

边栏推荐
- HarmonyOS第三次培训笔记
- 技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
- 【LeetCode】2. Valid Parentheses·有效的括号
- Lombok -- simplify code
- Industrial resilience
- Vertx restful style web router
- C WinForm framework
- Grpc message sending of vertx
- Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
- Vertx metric Prometheus monitoring indicators
猜你喜欢

【MySQL 12】MySQL 8.0.18 重新初始化

Common methods of file class

技术干货|昇思MindSpore NLP模型迁移之Roberta ——情感分析任务

【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表

PAT甲级 1027 Colors in Mars

技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型

Common architectures of IO streams

Analysis of the problems of the 7th Blue Bridge Cup single chip microcomputer provincial competition

Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition

Usage of requests module
随机推荐
基于RNA的新型癌症疗法介绍
Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience
Image recognition and detection -- Notes
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
IO stream system and FileReader, filewriter
[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
Hisat2 - stringtie - deseq2 pipeline for bulk RNA seq
Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
Segment read
Circuit, packet and message exchange
Industrial resilience
论文学习——鄱阳湖星子站水位时间序列相似度研究
JS monitors empty objects and empty references
Spa single page application
技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
opensips与对方tls sip trunk对接注意事项
Collector in ES (percentile / base)
Use of generics
Some experiences of Arduino soft serial port communication
The babbage industrial policy forum