Go語言程式:檢查字串是否包含子字串


子字串是指字串中較小的字串,Go 語言中的字串是字元的集合。由於 Go 語言中的字串是不可變的,因此在建立後不能修改它們。但是,連線或新增到現有字串可以建立新的字串。字串型別是 Go 語言中的內建型別,可以像其他任何資料型別一樣以多種方式使用。

語法

strings.Contains(str,substring string)

要確定字串是否包含特定子字串,可以使用 `Contains(s, substr string) bool` 函式。如果在提供的字串中找到子字串,則返回一個布林值,指示其存在。

strings.Index(str, substring string)

可以使用 `Index(s, str string) int` 函式來確定給定字串中指定子字串的第一個例項的索引。如果子字串不存在,則返回 -1;否則返回子字串在字串中的索引。

strings.Index(str, substring string)

可以使用 `Index(s, str string) int` 函式來確定給定字串中指定子字串的第一個例項的索引。如果子字串不存在,則返回 -1;否則返回子字串在字串中的索引。

演算法

  • 步驟 1 − 建立一個 `main` 包並宣告 `fmt`(格式包)和 `strings` 包

  • 步驟 2 − 建立一個 `main` 函式,並在該函式中建立一個字串 `mystr`

  • 步驟 3 − 使用字串函式檢查字串是否包含子字串

  • 步驟 4 − 列印輸出

示例 1

在這個示例中,我們將看到如何使用內建函式 `strings.Contains()` 檢查字串是否包含子字串。輸出將是一個布林值,列印在控制檯上。讓我們透過程式碼和演算法來輕鬆理解這個概念。

package main
import (
	"fmt"
	"strings"
)

func main() {
	mystr := "Hello,alexa!"  //create string
	fmt.Println("The string created here is:", mystr)
	substring := "alexa"   //hold substring
	fmt.Println("The substring from the string is:", substring)
	fmt.Println("Whether the substring is present in string or not?")
	fmt.Println(strings.Contains(mystr, substring))  //use built-in function to check if substring is present in string
}

輸出

The string created here is: Hello,alexa!
The substring from the string is: alexa
Whether the substring is present in string or not?
true

示例 2

在這個示例中,我們將看到如何使用 `strings.Index()` 函式檢查字串是否包含子字串。

package main
import (
	"fmt"
	"strings"
)
func main() {
	mystr := "Hello, alexa!" //create string
	fmt.Println("The string created here is:", mystr)
	substring := "alexa"   //substring 
	fmt.Println("The substring from the string is:", substring)
	fmt.Println("Whether the string contains the substring or not?")
	if strings.Index(mystr, substring) >= 0 {    //check if condition is true print the statement
		fmt.Println("The string contains the substring.")
	} else {
		fmt.Println("The string does not contain the substring.")
	}
}

輸出

The string created here is: Hello, alexa!
The substring from the string is: alexa
Whether the string contains the substring or not?
The string contains the substring.

示例 3

在這個示例中,我們將看到如何使用 `strings.Index()` 函式中的 for 迴圈來查詢字串是否包含子字串。

package main
import (
	"fmt"
	"strings"
)
func main() {
	mystr := "Hello, alexa!"  //create string
	fmt.Println("The string created here is:", mystr)
	substring := "alexa"  //hold substring
	fmt.Println("The substring present here is:", substring)
	fmt.Println("Whether the substring is present in string or not?")
	found := false
	for i := 0; i < len(mystr); i++ {
		if strings.Index(mystr[i:], substring) == 0 { //check if substring is present in string or not
			found = true
			break
		}
	}
	if found {
		fmt.Println("The string has substring in it.")
	} else {
		fmt.Println("The string does not have substring in it.")
	}
}

輸出

The string created here is: Hello, alexa!
The substring present here is: alexa
Whether the substring is present in string or not?
The string has substring in it.

結論

我們透過三個示例執行了檢查字串是否包含子字串的程式。在第一個示例中,我們使用了 `strings.Contains()` 函式;在第二個示例中,我們使用了 `strings.Index()` 函式;在第三個示例中,我們使用了前一個內建函式的 for 迴圈。

更新於:2023年2月1日

634 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.