string

Golang is a way to encapsulate strings in the form of built-in packages

package main import "fmt" func main() { str:="Hello,Golong" //for... For I,ch:=range STR {fmt.Printf("%d,%c\n", I,ch)} Len: = 0 FMT. Printf (" -- -- -- -- -- -- -- -- -- - "), for I ch: = range [] byte (STR) {FMT. Printf (" % d: % x \ n ", I, ch) len++} FMT) Printf (" byte length: %d\n",len) count:=0 for I,ch:=range[]rune(STR){fmt.Printf("%d:%c", I,ch) count++} fmt.Printf(" Length: %d\n",count)Copy the code

1.1 String handling functions

Sample Case string-related functions

Package main import (" FMT ""strings" "unicode") func testContain() fmt.Println(strings.Contains("string","ing")) //true fmt.Println(strings.Contains("seafood","foods")) //false } func TestIndex () {// Returns the position of another substring in the string fmt.println (strings.index ("string","ing")) //3 Println(strings.index ("string"," GGG ")) //-1} func testCount() {// Returns the number of substrings in the parent string fmt.Println(strings.Count("cheer","e")) //2 fmt.Println(strings.Count("string","e")) //0 } func testIndexFunc() { F := func(c rune) bool{return unicode.is (unicode.han,c)} Println(strings.indexFunc ("Hello, China ",f)) //6} func testLastIndex() {// Returns the last occurrence of the string fmt.Println(strings.LastIndex("this is a string","i")) //13 fmt.Println(strings.LastIndex("this is a string","z")) //-1 } func main() { testContain() testIndex() testCount() testIndexFunc() testLastIndex() }Copy the code

1.2 Common methods for splitting strings

Sample String segmentation

Package main import (" FMT ""strings" "unicode") func testFiles() { Println(strings.fields (" ABC 123 ABC xyz XYZ")) // Return a slice [ABC 123 ABC xyz xyz]} func testFilesFunc() {f:= func(c rune)bool { //return c== '-' return ! unicode.IsLetter(c)&&! Unicode.isnumber (c)} fmt.println (strings.fieldsfunc ("abc@123*ABC&xyz%XYZ",f)) // Return slice [ABC 123 ABC XYZ XYZ]} func TestSplitAfterN () {FMT. Printf (" % q \ n ", strings. SplitAfterN (" a, b, c ",, "", 2)) / / return [" a," "b, c"]} func testSplit () { FMT. Printf (" % q \ n ", and strings. The Split (" a, b, c ",, "")) / / return [" a" "b" "c"] FMT) Printf (" % q \ n ", and strings. The Split (" a man a plan a canal Panama ", "a")) / / return [" "" man" "plan" canal "panama"] FMT) Printf (" % q \ n ", and strings. The Split (" xyz ", "")) / / return [" x" "y" "z" "]" fmt.Printf("%q\n",strings.Split("","this is a string")) //[""] } func main() { testFiles() testFilesFunc() testSplitAfterN() testSplit() }Copy the code

1.3 The method of string case conversion

1.4 Common pruning strings

1.5 Common Comparison Strings

Parse the class function

Parse primarily converts strings to other types

The Format class function

The Format function is used to Format other types into strings. The following table lists the commonly used Format functions

Regxp regular expression package

Time package


Math package

Random number RAND package

Keyboard input

Scanln () function

Keyboard input function

package main

import "fmt"

func main()  {
	username:=""
	age:=0
	fmt.Scanln(&username,&age)
	fmt.Println("账号信息为:",username,age)
}

Copy the code

Keyboard input cases, guess the number of games




Copy the code