Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the function of GO language string processing Strings package

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the GO language string processing Strings package function how to use the relevant knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this GO language string processing Strings package function how to use the article will have a harvest, let's take a look at it.

Commonly used string processing function (1) Containsfunc Contains (s, substr string) bool

Function: whether the string s contains substr, and returns the value of Bool

The demonstration is as follows:

/ / find whether one string appears in another string str1: = "hello world" str2: = "g" / / Contains (the string to be found The returned value bool// is generally used for fuzzy lookup b: = strings.Contains (str1,str2) / / fmt.Println (b) if b {fmt.Println (found)} else {fmt.Println (not found)} (2) Joinfunc Join (a [] string, sep string) string

Function: string linking, linking slicea through sep

The demonstration is as follows:

/ / string slicing slice: = [] string {"123,456", "789"} / / fmt.Println (slice) / / Join// string concatenation str: = strings.Join (slice, ") fmt.Println (str) / / fmt.Printf ("% T\ n ", str)

The results are as follows:

123456789 (3) Indexfunc Index (s, substr string) int

Function: find the location of sep in the string s, return the location value, but cannot find-1

Str1: = "hello world" str2: = "e" / / find the first occurrence of a string in another string return value int subscript-1 cannot find I: = strings.Index (str1,str2) fmt.Println (I)

The results are as follows:

one

(4) Repeatfunc Repeat (s string, count int) string

Function: repeat the s string count times, and finally return the repeated string

The demonstration is as follows:

Str: = "Sexy netizens, named online." / / repeat a string n times str1: = strings.Repeat (str,100) fmt.Println (str1) (5) Replacefunc Replace (s, old, new string, n int) string

Function: in the s string, replace the old string with the new string. N represents the number of replacements, and less than 0 means all substitutions.

Str: = "Sexy netizens name Sexy online" / / string replacement masks sensitive words / / if the number of replacements is less than 0, all str1: = strings.Replace (str, "sexy", "*",-1) fmt.Println (str1)

The results are as follows:

* * netizens named it online *

(6) Splitfunc Split (s, sep string) [] string

Function: split the s string according to sep and return slice

/ / slice a string according to the flag bits str1: = "123456789@qq.com" slice: = strings.Split (str1, "@") fmt.Println (slice [0])

The results are as follows:

123456789

(7) Trimfunc Trim (s string, cutset string) string

Function: remove the string specified by cutset from the head and tail of the s string

Str: / / remove str1:= strings.Trim (str, "=") fmt.Println (str1) from the beginning and end of the string

The results are as follows:

A===u=ok

(8) Fieldsfunc Fields (s string) [] string

Function: remove the space character of the s string and return slice according to space division

Str: = "are you ok" / / the conversion from blanks to slices is generally used to count the number of words slice: = strings.Fields (str) fmt.Println (slice) string conversion

Through the above explanation, it is found that string processing is very important, and the GO language also provides functions for converting strings to and from other types. The corresponding string conversion functions are in the "strconv" package.

(1) Format

The Format series functions convert other types to strings.

/ / convert other types to the string Formatb: = falsestr: = strconv.FormatBool (true) fmt.Println (str) fmt.Printf ("% T\ n", str) str:= strconv.FormatInt (120 16fmt.Println 10) / / the binary in the computer can represent 2-36 28 10 16fmt.Println (str) / /'f 'print with 4 decimal places 64 in decimal mode and float64 to deal with str:= strconv.FormatFloat (3.14159). 4Jing 64) fmt.Println (str) str: = strconv.Itoa (123) fmt.Println (str) (2) Parse

Parse series functions convert strings to other types

/ / convert the string to another type Parseb,err: = strconv.ParseBool ("true") if erratic error nil {fmt.Println ("type conversion error")} else {fmt.Println (b) fmt.Printf ("% T\ n", b)} v strconv.ParseInt ("abc", 16pr) fmt.Println (v) _: = strconv.ParseFloat ("3.14159", 64) fmt.Println (v) v _: = strconv.Atoi ("123") fmt.Println (v) (3) Append

The Append series functions convert integers, etc., to strings and add them to an existing byte array.

Slice: = make ([] byte,0,1024) / / add other types as strings to the character slice slice = strconv.AppendBool (slice,false) slice = strconv.AppendInt (slice,123,2) slice = strconv.AppendFloat (slice,3.14159,'f',4,64) slice = strconv.AppendQuote (slice, "hello") fmt.Println (string (slice))

The corresponding result is:

False11110113.1416 "hello"

This is the end of the article on "how to use the functions of the GO language string processing Strings package". Thank you for reading! I believe that everyone has a certain understanding of "GO language string processing Strings package function how to use" knowledge, if you want to learn more knowledge, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report