golang- string

1. literal

  Literals are not seen through the outer package identifier is the initial value of the state memory

  var x string #x is variable

  x: = "1" #x is variable, "1" is a literal (naked eyes)

  const x int = 10 #x is constant, a literal 10

2. Create a string literal

  Explanatory string literal is written in double quotes, escape character may be used, a common \ t \ n

  The original string using anti-apostrophe is written, any characters are strings

3. string concatenation

  Splicing a variable buffer type using high efficiency operation to see more bytes packet

. 1  Package main
 2  
. 3  Import (
 . 4      " bytes " 
. 5      " FMT " 
. 6  )
 . 7  
. 8  FUNC main () {
 . 9      // string plus sign inefficient splice 
10      TEMP: = "" 
. 11      for I: = 0 ; I < 100 ; I ++ {
 12 is          TEMP + = " Zz " 
13 is      }
 14      fmt.Println (TEMP)
 15      // buffer is used to enhance the performance of the splice 
16      var Cache for bytes.Buffer states     //Statement buffer 
. 17      for I: = 0 ; I < 100 ; I ++ {
 18 is          cache.WriteString ( " Zz " )     // write the string type 
. 19      }
 20 is      fmt.Println (cache.String ())         // read character type of string 
21 is  
22 is }
View Code

4. Access string

  go use unicode characters, utf-8 encoding

  Indexed access string is obtained corresponding coding, may be used Switch characters% q

. 1  Package main
 2  
. 3 Import " FMT " 
. 4  
. 5  FUNC main () {
 . 6      // low splicing efficiency string plus sign 
. 7      A: = " ABCDEF " 
. 8      fmt.Println (A [ 0 ])                     // Print 10 hex coding 
. 9      fmt.Printf ( " % Q \ n- " , a [ . 1 ])         // printed coded characters corresponding to 
10      fmt.Println (a [ 0 : . 1 ])                     // slice is accessed character 
11 }
View Code

The string operations

  More strings packet reference standard library (returns the new string, bool value)

  1) determining whether the string starts with XX, XX whether to end

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "strings"
 6 )
 7 
 8 func main() {
 9     a := "ABCDEF"
10     fmt.Println(strings.HasPrefix(a, "A"))
11     fmt.Println(strings.HasSuffix(a, "A"))
12 }
View Code

  2) determining whether the string comprises a substring, or an element

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "strings"
 6 )
 7 
 8 func main() {
 9     a := "ABCDEF"
10     fmt.Println(strings.Contains(a,"A"))
11     fmt.Println(strings.Contains(a,"BCD"))
12 }
View Code

  3) unified case of the string

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "strings"
 6 )
 7 
 8 func main() {
 9     a := "ABcdeF"
10     fmt.Println(strings.ToUpper(a))
11     fmt.Println(strings.ToLower(a))
12 }
View Code

  4) double string

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "strings"
 6 )
 7 
 8 func main() {
 9     a := "AB"
10     fmt.Println(strings.Repeat(a,3))
11 }
View Code

  5) replace the string

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "strings"
 6 )
 7 
 8 func main() {
 9     a := "ABCDEFACF"
10     fmt.Println(strings.Replace(a,"A","Z",1))
11     fmt.Println(strings.ReplaceAll(a,"A","Z"))
12 }
View Code

  6) go blank string

 1 package main
 2 
 3 import (
 4    "fmt"
 5    "strings"
 6 )
 7 
 8 func main() {
 9    a := "                Top is theshy 666               "
10    fmt.Println(a)
11    fmt.Println(strings.TrimSpace(a))
12 }
View Code

  7) string divided into sections

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "reflect"
 6     "strings"
 7 )
 8 
 9 func main() {
10     a := "AB,CD,EF,ACF"
11     slice1 := strings.Split(a,",")
12     fmt.Println(len(slice1))
13     fmt.Println(reflect.TypeOf(slice1))
14 }
View Code

  8) The splice element may be a string of iterations, using the specified code division

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "strings"
 6 )
 7 
 8 func main() {
 9     a := "AB,CD,EF,ACF"
10     slice1 := strings.Split(a,",")
11     b := strings.Join(slice1,"+")
12     fmt.Println(b)
13 }
View Code

  9) Find contains substring, -1 does not contain

 1 package main
 2 
 3 import (
 4     "fmt"
 5     "strings"
 6 )
 7 
 8 func main() {
 9     str1:= "hello my name is quguanwen"
10     fmt.Println(strings.Index(str1,"quguanwen"))
11     fmt.Println(strings.Contains(str1,"quguanwen"))
12 }
View Code

 

Guess you like

Origin www.cnblogs.com/quguanwen/p/11754615.html