📅  最后修改于: 2021-01-02 09:08:11             🧑  作者: Mango
Go字符串是一系列可变宽度的字符。
Go字符串和文本文件占用更少的内存或磁盘空间。由于UTF-8是标准,所以Go不需要对字符串进行编码和解码。
Go字符串是值类型,并且是不可变的。这意味着,如果您创建一个字符串,则无法修改该字符串的内容。字符串的初始值默认为空“”。
package main
import ("fmt"
"reflect"
)
func main() {
var x string = "Hello World"
fmt.Println(x)
fmt.Println(reflect.TypeOf(x))
}
输出:
Hello World
string
package main
import "fmt"
func main() {
str := "I love my country"
fmt.Println(len(str))
}
输出:
17
package main
import "fmt"
func main() {
fmt.Println("Ascii value of A is ","A"[0])
}
输出:
Ascii value of A is 65
package main
import "fmt"
import "strings"
func main() {
str := "india"
fmt.Println(strings.ToUpper(str))
}
输出:
INDIA
package main
import "fmt"
import "strings"
func main() {
str := "INDIA"
fmt.Println(strings.ToLower(str))
}
输出:
india
package main
import "fmt"
import "strings"
func main() {
s := "INDIA"
fmt.Println(strings.HasPrefix(s,"IN"))
}
输出:
true
package main
import "fmt"
import "strings"
func main() {
s := "INDIA"
fmt.Println(strings.HasSuffix(s,"IA"))
}
输出:
true
package main
import "fmt"
import "strings"
func main() {
var arr = []string{"a","b","c","d"}
fmt.Println(strings.Join(arr,"*"))
}
输出:
a*b*c*d
package main
import "fmt"
import "strings"
func main() {
var str = "New "
fmt.Println(strings.Repeat(str,4))
}
输出:
New New New New
package main
import "fmt"
import "strings"
func main() {
str:= "Hi...there"
fmt.Println(strings.Contains(str,"th"))
}
输出:
true
package main
import "fmt"
import "strings"
func main() {
str:= "Hi...there"
fmt.Println(strings.Index(str,"th"))
}
输出:
5
package main
import "fmt"
import "strings"
func main() {
str:= "Hi...there"
fmt.Println(strings.Count(str,"e"))
}
输出:
2
package main
import "fmt"
import "strings"
func main() {
str:= "Hi...there"
fmt.Println(strings.Replace(str,"e","Z",2))
}
输出:
Hi...thZrZ
package main
import "fmt"
import "strings"
func main() {
str := "I,love,my,country"
var arr []string = strings.Split(str, ",")
fmt.Println(len(arr))
for i, v := range arr {
fmt.Println("Index : ", i, "value : ", v)
}
}
输出:
4
Index : 0 value : I
Index : 1 value : love
Index : 2 value : my
Index : 3 value : country
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Printf("%q\n", strings.Split("x,y,z", ","))
fmt.Printf("%q\n", strings.Split(" John and Jack and Johnny and Jinn ", "and"))
fmt.Printf("%q\n", strings.Split(" abc ", ""))
fmt.Printf("%q\n", strings.Split("", "Hello"))
}
输出:
["x" "y" "z"]
[" John " " Jack " " Johnny " " Jinn"]
[" " "a" "b" "c" " "]
[""]
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Compare("a", "b"))
fmt.Println(strings.Compare("a", "a"))
fmt.Println(strings.Compare("b", "a"))
}
输出:
-1
0
1
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.TrimSpace(" \t\n I love my country \n\t\r\n"))
}
输出:
I love my country
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ContainsAny("Hello", "A"))
fmt.Println(strings.ContainsAny("Hello", "o & e"))
fmt.Println(strings.ContainsAny("Hello", ""))
fmt.Println(strings.ContainsAny("", ""))
}
输出:
false
true
false
false