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

边栏推荐
- Go language foundation ----- 02 ----- basic data types and operators
- Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition
- 技术干货|昇思MindSpore NLP模型迁移之Roberta ——情感分析任务
- Common architectures of IO streams
- 昇思MindSpore再升级,深度科学计算的极致创新
- 技术干货|昇思MindSpore创新模型EPP-MVSNet-高精高效的三维重建
- Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
- Epoll related references
- 技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估
- 【LeetCode】2. Valid Parentheses·有效的括号
猜你喜欢

Comparison of advantages and disadvantages between most complete SQL and NoSQL

技术干货|昇思MindSpore NLP模型迁移之Bert模型—文本匹配任务(二):训练和评估

The embodiment of generics in inheritance and wildcards

Topic | synchronous asynchronous

Leetcode 213: looting II

Traversal in Lucene

VMWare网络模式-桥接,Host-Only,NAT网络

Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!

昇思MindSpore再升级,深度科学计算的极致创新

截图工具Snipaste
随机推荐
Lucene skip table
Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
Lucene merge document order
项目经验分享:基于昇思MindSpore实现手写汉字识别
Spa single page application
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
Circuit, packet and message exchange
Grpc message sending of vertx
項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
Some basic operations of reflection
Implementation of breadth first in aggregation in ES
Read config configuration file of vertx
Go language foundation ----- 07 ----- method
[coppeliasim4.3] C calls UR5 in the remoteapi control scenario
Leetcode 213: looting II
Rabbit MQ message sending of vertx
c语言指针的概念
An overview of IfM Engage
Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task