当前位置:网站首页>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)
}
边栏推荐
- JUnit unit test of vertx
- 【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
- Longest common prefix and
- Lombok -- simplify code
- PAT甲级 1032 Sharing
- Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition
- PAT甲级 1030 Travel Plan
- Lombok cooperates with @slf4j and logback to realize logging
- Talk about floating
- Common methods of file class
猜你喜欢
【MySQL 12】MySQL 8.0.18 重新初始化
Shengsi mindspire is upgraded again, the ultimate innovation of deep scientific computing
Comparison of advantages and disadvantages between most complete SQL and NoSQL
Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!
【LeetCode】3. Merge Two Sorted Lists·合并两个有序链表
Arduino Serial系列函数 有关print read 的总结
项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass
The embodiment of generics in inheritance and wildcards
Inverted chain disk storage in Lucene (pfordelta)
Es writing fragment process
随机推荐
Reconnaissance et détection d'images - Notes
[mindspire paper presentation] summary of training skills in AAAI long tail problem
项目经验分享:基于昇思MindSpore实现手写汉字识别
Qtip2 solves the problem of too many texts
[set theory] order relation (partial order relation | partial order set | example of partial order set)
你开发数据API最快多长时间?我1分钟就足够了
List exercises after class
The concept of C language pointer
Vertx's responsive MySQL template
Project experience sharing: Based on mindspore, the acoustic model is realized by using dfcnn and CTC loss function
Download address collection of various versions of devaexpress
技术干货|AI框架动静态图统一的思考
Analysis of the problems of the 7th Blue Bridge Cup single chip microcomputer provincial competition
Chapter VI - Containers
不出网上线CS的各种姿势
圖像識別與檢測--筆記
What did the DFS phase do
An overview of IfM Engage
Introduction of transformation flow
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型