Common methods of golang strings

##A string exists in another string

package main
import "fmt"
import "strings"

// func Contains(s, substr string) bool This function is to find whether a certain character exists in this string, and returns true. 
// Verify whether the second parameter exists in the first string 
func  main ( ) { 
    fmt . Println ( strings . Contains ( "chain" ,  "ch" ) ) 
    fmt . Println ( strings . Contains ( "ch" ,  "chain" ) ) 
    fmt . Println ( strings . Contains ( "chain" ,  "" ) )    //true 
    fmt . Println ( strings . Contains ( "" ,  "" ) )            //true Pay special attention to 
    fmt . Println ( strings . Contains ( "I am Chinese" ,  "I" ) )      //true 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Whether the query string contains more than one character

package main
import "fmt"
import "strings"
func main(){
    fmt.Println(strings.ContainsAny("chain", "b"))        // false
    fmt.Println(strings.ContainsAny("chain", "c & i")) // true
    fmt.Println(strings.ContainsAny("chain", ""))          // false
    fmt.Println(strings.ContainsAny("", ""))             // false}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

##The number of occurrences of a string in another string

package mainimport "fmt"import "strings"
func main(){
 fmt.Println(strings.Count("chainn", "nn")) //1
 fmt.Println(strings.Count("chainn", "n"))  //2
 fmt.Println(strings.Count("chain", ""))  // before & after each rune result:6}
  • 1
  • 2
  • 3
  • 4
  • 5

Find the first occurrence of a string within another string

package mainimport "fmt"import "strings"
func main(){
  fmt.Println(strings.Index("chain", "h")) //1
  fmt.Println(strings.Index("chainn", "n")) //4
  fmt.Println(strings.Index("chainn", "q")) //-1 
  fmt . Println ( strings . Index ( "I am Chinese" ,  "中" ) )      // return 6}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

// another

package main
 import  "fmt" 
import  "strings" 
func  main ( ) { 
    fmt . Println ( strings . IndexAny ( "chainn" ,  "n" ) )  //4 
    fmt . Println ( strings . IndexAny ( "I am Chinese" ,  "中" ) )  // return 6 if exists 
    fmt . Println ( strings . IndexAny ( "I am Chinese" , "and") )  // returns -1 if exists}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Find the last occurrence of a string within another string

package mainimport "fmt"import "strings"
func main() {
  fmt.Println(strings.LastIndex("chaina", "a")) // 5}
  • 1
  • 2
  • 3

String comparison , ignoring case

package mainimport "fmt"import "strings"
func main() {
  fmt.Println(strings.EqualFold("chain", "CHAIN")) // true
  fmt.Println(strings.EqualFold("chain", "CHAI")) // false}
  • 1
  • 2
  • 3
  • 4

Concatenate an array of strings into a string according to the specified delimiter

package mainimport "fmt"import "strings"
func main() {
    s := []string{"foo", "bar", "baz"}
    fmt.Println(strings.Join(s, ", ")) // 返回字符串:foo, bar, baz}
  • 1
  • 2
  • 3
  • 4

string replacement

f

unc Replace ( s , old ,  new  string , count int ) string returns a new string, the parameter s is the original string, old is the string to be replaced, new is the string to replace old, and count is The number of replacements, or - 1 for all replacements.

package mainimport "fmt"import "strings"
func main() {
    fmt.Println(strings.Replace("oink oink oink", "k", "ky", 5))
    fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
    fmt.Println(strings.Replace("oink oink oink", "k", "ky", -1))}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

The string is divided according to the specified string to generate a string array

package mainimport "fmt"import "strings"
func main() {
    fmt.Printf("%q\n", strings.Split("a,b,c", ","))
    fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
    fmt.Printf("%q\n", strings.Split(" xyz ", ""))
    fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Remove leading and trailing whitespace from a string

package mainimport "fmt"import "strings"func main() {
    fmt.Println( strings.TrimSpace("\t\n a lone gopher \n\t\r\n"))}
  • 1
  • 2

Tags: Common methods of golang strings

go golang

Related: Common methods of golang strings