Skip to content

Latest commit

 

History

History
44 lines (35 loc) · 667 Bytes

1.5:String.md

File metadata and controls

44 lines (35 loc) · 667 Bytes

Declare ans initialize:

str := "This\nis\na\nstring"
str := `This
is
a
String`

Traverse:

for _, ch := range str {
    fmt.Println(string(ch))
}
str := "Hi, this is kechang"
arr := []rune(str)
for _, ch := range arr {
    fmt.Println(string(ch))
}

Modify: In Golang, We can't modify a string directly.

str := "Hi  this is kechang"
s := []rune(str)
s[2] = ','

for _, ch := range s {
    fmt.Printf(string(ch))
}

PS: In Golang, rune is an alias for int32 ans it is used primarily to handle UTF-8 characters, and a byte is an alias for uint8 and it used primarily to handle ASCII characters.