Làm thế nào để bạn đếm sự xuất hiện của một chuỗi con trong một chuỗi?

Để đếm hoặc lấy số lần xuất hiện của một chuỗi con trong một chuỗi bằng ngôn ngữ Go, hãy gọi hàm Count của gói

package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
0 và chuyển chuỗi và chuỗi con làm đối số cho nó

Cú pháp đếm số lần xuất hiện của xâu con

package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
1 trong xâu
package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
2 sử dụng hàm Count

strings.Count[str, substr]

Hàm đếm trả về một số nguyên biểu thị số lần xuất hiện không trùng nhau của chuỗi con trong chuỗi

ví dụ

Trong ví dụ sau, chúng tôi lấy hai chuỗi,

package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
2 và
package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
1, rồi đếm số lần xuất hiện của
package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
1 trong
package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
2 bằng cách sử dụng hàm Count

ví dụ. đi

package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}

đầu ra

The number of occurrences of substring in the string is:  2

Bây giờ, chúng ta hãy lấy các giá trị cho str và chất nền, sao cho chất nền có các lần xuất hiện chồng chéo trong str. Chúng tôi biết rằng số lượng chỉ trả về các lần xuất hiện không chồng chéo

ví dụ. đi

package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "ababababa"
	substr := "aba"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}

package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
5 lần xuất hiện chồng chéo của
package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
6 trong
package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
7, nhưng chỉ có
package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
8 lần xuất hiện không chồng chéo

đầu ra

The number of occurrences of substring in the string is:  2

Phần kết luận

Trong Hướng dẫn về Golang này, chúng ta đã học cách đếm số lần xuất hiện không trùng nhau của một chuỗi con trong một chuỗi bằng ngôn ngữ lập trình Go, với sự trợ giúp của các chương trình ví dụ

Phương thức

package main

import [
	"fmt"
	"strings"
]

func main[] {
	str := "Good Morning! Good Day!"
	substr := "Good"
	count := strings.Count[str, substr]
	fmt.Println["The number of occurrences of substring in the string is: ", count]
}
9 trả về chỉ mục xuất hiện đầu tiên của chuỗi đã cho trong một chuỗi và trả về
The number of occurrences of substring in the string is:  2
0 nếu không tìm thấy chuỗi. Nó có thể được sử dụng trong vòng lặp while để tìm tất cả các lần xuất hiện của một chuỗi con trong một chuỗi, như minh họa bên dưới

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

sử dụng Hệ thống;

 

công khai tĩnh lớp StringExtensions

{

    công khai tĩnh int Số lượng[this string input, string substr]

    {

        int freq = 0;

 

        int index = đầu vào.IndexOf[substr];

        while [index >= 0]

        {

            chỉ mục = đầu vào. IndexOf[substr, index . + substr.Độ dài];

            tần số++;

        }

        trả về tần số;

    }

}

 

lớp công khai Ví dụ

{

    công khai tĩnh vô hiệu Chính[]

    {

        chuỗi đầu vào = "Xin chào thế giới"

Chủ Đề