packagemainimport("fmt""strings")funcmain(){varstrstring="This is an example of a string"fmt.Printf("T/F? Does the string \"%s\" have prefix %s? ",str,"Th")fmt.Printf("%t\n",strings.HasPrefix(str,"Th"))}//Output:
//T/F? Does the string "This is an example of a string" have prefix Th? true
更一般化的,Contains 判断字符串 s 是否包含 substr:
1
strings.Contains(s,substrstring)bool
1.2 获取子字符串的位置
Index 返回字符串 str 在字符串 s 中的索引(str 的第一个字符的索引),-1 表示字符串 s 不包含字符串 str:
1
strings.Index(s,strstring)int
LastIndex 返回字符串 str 在字符串 s 中最后出现位置的索引(str 的第一个字符的索引),-1 表示字符串 s 不包含字符串 str:
packagemainimport("fmt""strings")funcmain(){varstrstring="Hi, I'm Marc, Hi."fmt.Printf("The position of \"Marc\" is: ")fmt.Printf("%d\n",strings.Index(str,"Marc"))fmt.Printf("The position of the first instance of \"Hi\" is: ")fmt.Printf("%d\n",strings.Index(str,"Hi"))fmt.Printf("The position of the last instance of \"Hi\" is: ")fmt.Printf("%d\n",strings.LastIndex(str,"Hi"))fmt.Printf("The position of \"Burger\" is: ")fmt.Printf("%d\n",strings.Index(str,"Burger"))}/*Output:
The position of "Marc" is: 8
The position of the first instance of "Hi" is: 0
The position of the last instance of "Hi" is: 14
The position of "Burger" is: -1
*/
packagemainimport("fmt""strings")funcmain(){varstrstring="Hello, how is it going, Hugo?"varmanyG="gggggggggg"fmt.Printf("Number of H's in %s is: ",str)fmt.Printf("%d\n",strings.Count(str,"H"))fmt.Printf("Number of double g's in %s is: ",manyG)fmt.Printf("%d\n",strings.Count(manyG,"gg"))}/*Output:
Number of H's in Hello, how is it going, Hugo? is: 2
Number of double g’s in gggggggggg is: 5
*/
1.4 替换与重复
Replace 用于将字符串 str 中的前 n 个字符串 old 替换为字符串 new,并返回一个新的字符串,如果 n = -1 则替换所有字符串 old 为字符串 new:
1
strings.Replace(str,old,new,n)string
Repeat 用于重复 count 次字符串 s 并返回一个新的字符串:
1
strings.Repeat(s,countint)string
示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
packagemainimport("fmt""strings")funcmain(){varorigSstring="Hi there! "varnewSstringnewS=strings.Repeat(origS,3)fmt.Printf("The new repeated string is: %s\n",newS)}//Output:
//The new repeated string is: Hi there! Hi there! Hi there!
packagemainimport("fmt""strings")funcmain(){varorigstring="Hey, how are you George?"varlowerstringvarupperstringfmt.Printf("The original string is: %s\n",orig)lower=strings.ToLower(orig)fmt.Printf("The lowercase string is: %s\n",lower)upper=strings.ToUpper(orig)fmt.Printf("The uppercase string is: %s\n",upper)}/*Output:
The original string is: Hey, how are you George?
The lowercase string is: hey, how are you george?
The uppercase string is: HEY, HOW ARE YOU GEORGE?
*/
packagemainimport("fmt""strings")funcmain(){str:="The quick brown fox jumps over the lazy dog"sl:=strings.Fields(str)fmt.Printf("Splitted in slice: %v\n",sl)for_,val:=rangesl{fmt.Printf("%s - ",val)}fmt.Println()str2:="GO1|The ABC of Go|25"sl2:=strings.Split(str2,"|")fmt.Printf("Splitted in slice: %v\n",sl2)for_,val:=rangesl2{fmt.Printf("%s - ",val)}fmt.Println()str3:=strings.Join(sl2,";")fmt.Printf("sl2 joined by ;: %s\n",str3)}/*Output:
Splitted in slice: [The quick brown fox jumps over the lazy dog]
The - quick - brown - fox - jumps - over - the - lazy - dog -
Splitted in slice: [GO1 The ABC of Go 25]
GO1 - The ABC of Go - 25 -
sl2 joined by ;: GO1;The ABC of Go;25
*/
packagemainimport"fmt"varfirstName,lastNamestringfuncmain(){fmt.Println("Please enter your full name: ")fmt.Scanln(&firstName,&lastName)// fmt.Scanf("%s %s", &firstName, &lastName)
}
packagemainimport"fmt"funcmain(){varnamestringvarageintn,err:=fmt.Sscanf("Kim is 22 years old","%s is %d years old",&name,&age)iferr!=nil{panic(err)}fmt.Printf("%d: %s, %d\n",n,name,age)}//Output
2:Kim,22
packagemainimport("fmt""io""os")funcmain(){constname,age="Kim",22fmt.Printf("%s is %d years old.\n",name,age)s:=fmt.Sprintf("%s is %d years old.\n",name,age)io.WriteString(os.Stdout,s)// Ignoring error for simplicity.
n,err:=fmt.Fprintf(os.Stdout,"%s is %d years old.\n",name,age)iferr!=nil{fmt.Fprintf(os.Stderr,"Fprintf: %v\n",err)}fmt.Printf("%d bytes written.\n",n)}//Output
Kimis22yearsold.Kimis22yearsold.Kimis22yearsold.21byteswritten.
packagemainimport("fmt""bufio""os")funcmain(){inputReader:=bufio.NewReader(os.Stdin)fmt.Println("Please enter some input: ")input,err:=inputReader.ReadString('\n')iferr==nil{fmt.Printf("The input was: %s\n",input)}}
packagemainimport("bufio""fmt""io""os")funcmain(){inputFile,inputError:=os.Open("input.dat")ifinputError!=nil{fmt.Printf("An error occurred on opening the inputfile\n"+"Does the file exist?\n"+"Have you got acces to it?\n")return// exit the function on error
}deferinputFile.Close()inputReader:=bufio.NewReader(inputFile)for{inputString,readerError:=inputReader.ReadString('\n')fmt.Printf("The input was: %s",inputString)ifreaderError==io.EOF{return}}}