1024programmer Java Summary of usage of strings package in go language

Summary of usage of strings package in go language

Functions and methods in the strings package

// strings.go

————————————————– ————-

// Count counts the number of non-overlapping numbers of the string sep in s
// If sep is an empty string, return the number of characters (non-bytes) in s + 1
// Implemented using Rabin-Karp algorithm
func Count(s, sep string) int

func main() {
s := “Hello, world!!!!!”
n := strings.Count(s, “!”)
fmt.Println(n) // 5
n = strings.Count(s, “!!”)
fmt.Println(n) // 2
}

————————————————– ————-

//Contains determines whether the string s contains a substring substr
// If substr is empty, return true
func Contains(s, substr string) bool

func main() {
s := “Hello, world!!!!!”
b := strings.Contains(s, “!!”)
fmt.Println(b) // true
b = strings.Contains(s, “!?”)
fmt.Println(b) // false
b = strings.Contains(s, “”)
fmt.Println(b) // true
}

————————————————– ————-

//ContainsAny determines whether the string s contains any character in chars
// If chars is empty, return false
func ContainsAny(s, chars string) bool

func main() {
s := “Hello, world!”
b := strings.ContainsAny(s, “abc”)
fmt.Println(b) // false
b = strings.ContainsAny(s, “def”)
fmt.Println(b) // true
b = strings.Contains(s, “”)
fmt.Println(b) // true
}

————————————————– ————-

// ContainsRune determines whether the string s contains the character r
func ContainsRune(s string, r rune) bool

func main() {
s := “Hello, world!”
b := strings.ContainsRune(s, ‘\n’)
fmt.Println(b) // false
b = strings.ContainsRune(s, ‘boundary’)
fmt.Println(b) // true
b = strings.ContainsRune(s, 0)
fmt.Println(b) // false
}

————————————————– ————-

// Index returns the position of the first occurrence of the substring sep in the string s
// Returns -1 if not found, or 0 if sep is empty.
// Implemented using Rabin-Karp algorithm
func Index(s, sep string) int
func main() {
s := “Hello, world!”
i := strings.Index(s, “h”)
fmt.Println(i) // -1
i = strings.Index(s, “!”)
fmt.Println(i) // 12
i = strings.Index(s, “”)
fmt.Println(i) // 0
}

————————————————– ————-

//LastIndex returns the position of the last occurrence of the substring sep in the string s
// If not found, return -1, if sep is empty, return the length of the string
// Implemented using naive string comparison algorithm
func LastIndex(s, sep string) int

func main() {
s := “Hello, world! Hello!”
i := strings.LastIndex(s, “h”)
fmt.Println(i) // -1
i = strings.LastIndex(s, “H”)
fmt.Println(i) // 14
i = strings.LastIndex(s, “”)
fmt.Println(i) // 20
}

————————————————– ————-

// IndexRune returns the position of the first occurrence of character r in string s
// If not found, return -1
func IndexRune(s string, r rune) int
func main() {
s := “Hello, world! Hello!”
i := strings.IndexRune(s, ‘\n’)
fmt.Println(i) // -1
i = strings.IndexRune(s, ‘boundary’)
fmt.Println(i) // 9
i = strings.IndexRune(s, 0)
fmt.Println(i) // -1
}

————————————————– ————-

// IndexAny returns the position of the first occurrence of any character in string chars in string s
// If not found, return -1, if chars is empty, return -1
func IndexAny(s, chars string) int

func main() {
s := “Hello, world! Hello!”
i := strings.IndexAny(s, “abc”)
fmt.Println(i) // -1
i = strings.IndexAny(s, “dof”)
fmt.Println(i) // 1
i = strings.IndexAny(s, “”)
fmt.Println(i) // -1
}

————————————————– ————-

//LastIndexAny returns the position of the last occurrence of any character in string chars in string s
// If not found, return -1, if chars is empty, also return -1
func LastIndexAny(s, chars string) int

func main() {
s :=mt.Printf(“%q\n”, b)
r.Seek(-5, 1) // Move the index position back
r.Read(b) // Continue reading
fmt.Printf(“%q\n”, b)
}

————————————————– ————-

//WriteTo writes the data after r.i into interface w
func (r *Reader) WriteTo(w io.Writer) (n int64, err error)

func main() {
s := “Hello World!”
//Create Reader
r := strings.NewReader(s)
// Create a bytes.Buffer object, which implements the io.Writer interface
buf := bytes.NewBuffer(nil)
//Write the data in r into buf
r.WriteTo(buf)
fmt.Printf(“%q\n”, buf) // “Hello World!”
}

================================================ =============

// replace.go

————————————————– ————-

// Replacer performs a replacement operation based on a replacement list
type Replacer struct {
Replace(s string) string
WriteString(w io.Writer, s string) (n int, err error)
}

————————————————– ————-

// NewReplacer creates a Replacer object through the “replacement list”.
// Replace in the order in the “replacement list”, only replacing non-overlapping parts.
// If the number of parameters is not even, throw an exception.
// If there are the same “find items” in the “replacement list”, subsequent repeated “find items” will be ignored
func NewReplacer(oldnew …string) *Replacer

————————————————– ————-

// Replace returns the result of “find and replace” on s
// Replace uses the Boyer-Moore algorithm, which is very fast
func (r *Replacer) Replace(s string) string

func main() {
srp := strings.NewReplacer(“Hello”, “Hello”, “World”, “世界”, “!”, “!”)
s := “Hello World!Hello World!hello world!”
rst := srp.Replace(s)
fmt.Print(rst) // Hello world! Hello World! hello world!
}
Note: Both writing methods are acceptable.
func main() {

wl := []string{“Hello”, “Hi”, “Hello”, “Hello”}
srp := strings.NewReplacer(wl…)
s := “Hello World! Hello World! hello world!”
rst := srp.Replace(s)
fmt.Print(rst) // Hi World! Hi World! hello world!
}

————————————————– ————-

//WriteString performs a “find and replace” on s and writes the result into w
func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)

func main() {
wl := []string{“Hello”, “Hello”, “World”, “世界”, “!”, “!”}
srp := strings.NewReplacer(wl…)
s := “Hello World!Hello World!hello world!”
srp.WriteString(os.Stdout, s)
// Hello World! Hello World! hello world!
}

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/784263

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索