当前位置:网站首页>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)
}

边栏推荐
- Lucene skip table
- Spa single page application
- 技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
- Various postures of CS without online line
- 截图工具Snipaste
- Go language foundation ----- 01 ----- go language features
- JS monitors empty objects and empty references
- PAT甲级 1029 Median
- Lucene merge document order
- 【开发笔记】基于机智云4G转接板GC211的设备上云APP控制
猜你喜欢

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

最全SQL与NoSQL优缺点对比

PAT甲级 1032 Sharing

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

PAT甲级 1027 Colors in Mars

Why is data service the direction of the next generation data center?

Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task

技术干货|关于AI Architecture未来的一些思考

你开发数据API最快多长时间?我1分钟就足够了

Es writing fragment process
随机推荐
技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
Download address collection of various versions of devaexpress
Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience
Application of pigeon nest principle in Lucene minshouldmatchsumscorer
Arduino 软串口通信 的几点体会
Mail sending of vertx
Implementation of breadth first in aggregation in ES
Traversal in Lucene
Spa single page application
技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
Beginners use Minio
【开发笔记】基于机智云4G转接板GC211的设备上云APP控制
docket
Usage of requests module
Responsive MySQL of vertx
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
Warehouse database fields_ Summary of SQL problems in kingbase8 migration of Jincang database
[Development Notes] cloud app control on device based on smart cloud 4G adapter gc211
Jeecg request URL signature
JUnit unit test of vertx