go language string split

go language string following sub-division:

1, is divided in accordance with the spacebar

  方法:strings.Fields(s string) []string

    s: string to split

    Returns a value of [] String

  例:  s:=" ab cd          ef gh ij kl "

      arr:=strings.Fields(s)

      fmt.Printf("arr:%q\n",arr)

  结果为:arr:["ab" "cd" "ef" "gh" "ij" "kl"]

  Note: 1, the segmentation result is not comprise an empty string, such as the previously ab a space behind kl has a space, but the result of the segmentation is: [ "ab" "cd" "ef" "gh" "ij" " kl "], rather than: [" "" ab "" cd "" ef "" gh "" ij "" kl "" "]. Dividing a length of 6, 8 while not

    2, if the string to be split if a plurality of successive spaces will occur, and will not be repeatedly divided, for example, a plurality of embodiments appeared consecutive spaces between cd and ef, the result is: [ "ab" " cd "" ef "" gh "" ij "" kl "], rather than: [" ab "" cd "" "" "" "" "" ef "" gh "" ij "" kl "]

2, according to a further string of the character string dividing

  方法:strings.Split(s,sep string) []string

    s: string to split

    sep: string to be removed

    Returns a value of [] String

  例:  s:="iiaiibiiciiiidiiii"

      sep:="ii"

      arr:=strings.Split(s,sep)

      fmt.Println("arr:",arr)

  结果为:arr:["" "a" "b" "c" "" "d" "" ""]

  注:当sep=""时,会根据""进行分割,即结果为:arr:["i" "i" "a" "i" "i" "b" "i" "i" "c" "i" "i" "i" "i" "d" "i" "i" "i" "i"]

 

Guess you like

Origin www.cnblogs.com/yangzhan/p/11564044.html