Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

We also use existing libraries a lot in Golang. Let’s take a look at some of the golang libraries we use:

String correlation

strings

Normal operations
func main(a) {
  a := "Sizzle sizzle."
  b := "Ha ha ha."
  fmt.Println(strings.Compare(a,b))// String comparison if equal is 0, not equal -1
  fmt.Println(strings.Contains(a,"Zi"))// Whether to contain a certain character or string
  fmt.Println(strings.ContainsAny(a,"Sizzle and slip."))// If any character exists, it returns true
  fmt.Println(strings.Count(a,"Zi"))// Number of occurrences of the string, if the following character is empty return string length +1
}
Copy the code
String splitting

Contains three sets of six functions, Fields and FieldsFunc, Split and SplitAfter, SplitN and SplitAfterN

Split(s, sep) and SplitN(s, sep, -1) are identical; SplitAfter(s, sep) and SplitAfterN(s, sep, -1) are the same.

func main(a) {
  // Slices can be printed using FMT.Printf("%q", slices)
  s := "this is my world"
  strings.Fields(s)// Split strings with Spaces
  strings.FieldsFunc(" foo bar baz ", unicode.IsSpace)// Specify character split

  s1 := "Today, today, is a good, day, son."
  fmt.Printf("%q",strings.Split(s1, ","))// If it is null, each character is split
  fmt.Printf("%q",strings.SplitAfter(s1,"s"))// Keep the conditional string after splitting
  fmt.Printf("%q",strings.SplitN(s1,",".2))//n indicates the size of the returned slice
  fmt.Printf("%q",strings.SplitAfterN(s1,",".2))//n indicates the size of the returned slice

}
Copy the code
Judge the prefix and suffix
func main(a){
  s := "So it's okay."
  strings.HasSuffix(s, "The")// Determine the suffix
  strings.HasPrefix(s,"What")// Determine the prefix
 }
Copy the code
String splicing
  // Use + concatenation
  pinjie1 := "Perfect world."+"How are you?"
  fmt.Println(pinjie1)

  // Use Sprintf splicing
  s := "Perfect world."
  res := fmt.Sprintf("%s%s", s, "Ha ha ha.")
  fmt.Println(res)

  // Use strings.join to join
  s1 := []string{s,"Hey, hey, ha, ha."}
  join := strings.Join(s1, "= = = =")
  fmt.Println(join)

  // Use builder, the official recommendation
  s3 := "hello"
  s4 := "world"
  var build strings.Builder
  build.WriteString(s3)
  build.WriteString(s4)
  s5 := build.String()
  fmt.Println(s5)
Copy the code
Other operating
func main(a) {
  s := "are you ok"
  strings.Index(s, "r")// The position of a character in a string may be two characters long if it is a Chinese character
  fmt.Println("ba"+strings.Repeat("na".2))// Repeat the string n times
  fmt.Println(strings.Replace(s,"a"."b".1))// Replace several strings
  fmt.Println(strings.ReplaceAll(s,"o"."b"))// Replace all strings
  fmt.Println(strings.ToLower(s))/ / lowercase
  fmt.Println(strings.ToUpper(s))/ / caps
  fmt.Println(strings.Title(s))//title will capitalize the first letter
  fmt.Println(strings.Trim(s,"a"))// Remove a character on the left or right side
}
Copy the code

strconv

Atoi

A string is converted to an int

Itoa

Int is converted to string

ParaseType series

Converts string to the specified Type

b, err := strconv.ParseBool("true")
f, err := strconv.ParseFloat("3.1415".64)
i, err := strconv.ParseInt("- 2".10.64)
u, err := strconv.ParseUint("2".10.64)
Copy the code
FormatType series

The ability to format data of a given type into string data

s1 := strconv.FormatBool(true)
s2 := strconv.FormatFloat(3.1415.'E'.- 1.64)
s3 := strconv.FormatInt(2 -.16)
s4 := strconv.FormatUint(2.16)
Copy the code