Summary of string lookup methods in the Go language
- 2020-05-30 20:23:04
- OfStack
func Contains(s, substr string) bool is a function that looks up whether a character exists in the string and returns true if it does
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Contains("widuu", "wi")) //true
fmt.Println(strings.Contains("wi", "widuu")) //false
}
func ContainsAny(s, chars string) bool this is the query string contains more than one character
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ContainsAny("widuu", "w&d")) //true
}
3.func ContainsRune(s string, r rune) bool, which of course is whether the rune type is included in the string, where rune is utf8.RUneCountString can fully represent all Unicode characters
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ContainsRune("widuu", rune('w'))) //true
fmt.Println(strings.ContainsRune("widuu", 20)) //fasle
}
func Count(s, sep string) int is the output of how many matched characters in a string
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Count("widuu", "uu")) //1
fmt.Println(strings.Count("widuu", "u")) //2
}
func Index(s, sep string) int is a function that looks up a string and returns the current location, all of which is of type string, and then int's location
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Index("widuu", "i")) //1
fmt.Println(strings.Index("widuu", "u")) //3
}
func IndexAny(s, chars string) int this function is a lookup of the first occurrence of the string, and returns -1 if it does not exist
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.IndexAny("widuu", "u")) //3
}
7.func IndexByte(s string, c byte) int, this function function is still looking for the location of the first thick line, only this time C is of type byte, the return location is found, but -1 cannot be found
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.IndexByte("hello xiaowei", 'x')) //6
}
8.func IndexRune(s string, r rune) int, again find the location, only this time it is of type rune
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.IndexRune("widuu", rune('w'))) //0
}
func IndexFunc(s string, f func(rune) bool) int
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.IndexFunc("nihaoma", split)) //3
}
func split(r rune) bool {
if r == 'a' {
return true
}
return false
}
10.func LastIndex(s, sep string) int (s, sep string) int (func, sep string
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.LastIndex("widuu", "u")) // 4
}
11.func LastIndexAny(s, chars string) int this is the opposite of indexAny and is the last one to look for
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.ContainsAny("widuu", "w&d")) //true
}
0