Go 語言程式檢查字串是否包含指定子字串
什麼是字串和子字串?
在 Go 中,子字串是指較大字串的一部分。它透過提供起始索引和長度來指定,包含從起始索引開始到指定長度的原始字串中的字元。子字串中的字元仍然是原始字串的一部分,並共享相同的記憶體。
在 Go 中,字串是字元序列。它是一種不可變的資料型別,這意味著一旦建立字串,就無法修改它。字串用雙引號 ("") 括起來,可以包含字母、數字和符號的任意組合。
在本文中,我們將使用 Go 語言中的 for 迴圈和庫函式來實現此結果。子字串可以被認為是完整字串的子集。子字串的元素始終存在於原始字串中。
方法 1:使用使用者定義函式
在這種方法中,我們將建立外部使用者定義函式,並將字串和子字串作為引數傳遞給函式。然後,根據函式返回的值,我們將得出結論。
演算法
步驟 1 - 首先,我們需要匯入 fmt 包。
步驟 2 - 然後,我們需要開始 containsSubstring() 函式。此函式接受字串以及要檢查的子字串作為引數,並根據是否找到子字串返回布林值。
步驟 3 - 使用 for 迴圈遍歷給定的字串。並使用 if 條件檢查子字串的當前字元是否出現在字串的任何位置。
步驟 4 - 如果該值是字串的一部分,則返回 true,否則返回 false。
步驟 5 - 現在,開始 main() 函式。在此函式中,初始化一個字串以及子字串,併為其賦值。
步驟 6 - 透過將字串和子字串作為引數傳遞給函式來呼叫 containsSubstring() 函式。
步驟 7 - 如果函式返回的值為 true,則列印子字串在字串中找到,否則列印子字串未在字串中找到。
步驟 8 - 透過獲取更多示例重複此過程,並在螢幕上列印相應的結果。
示例
在此示例中,我們將使用 for 迴圈來檢查字串是否包含指定的子字串。
package main
import "fmt"
func containsSubstring(str, substr string) bool {
for i := 0; i < len(str)-len(substr)+1; i++ {
if str[i:i+len(substr)] == substr {
return true
}
}
return false
}
func main() {
fmt.Println("Example 1:")
var str string = "Live young live free"
var substr string = "free"
if containsSubstring(str, substr) {
fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
} else {
fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
}
fmt.Println()
fmt.Println("Example 2:")
substr = "world"
if containsSubstring(str, substr) {
fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
} else {
fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
}
}
輸出
Example 1: The string 'Live young live free' contains the substring 'free'. Example 2: The string 'Live young live free' does not contain the substring 'world'.
方法 2:使用內部函式()
在這種方法中,我們將使用兩個不同的函式,即 contains() 和 index,來檢查字串是否包含指定的子字串。
語法
func Contains(str, substr string) bool
contain() 函式存在於 strings 包中,用於檢查給定的子字串是否存在於字串中。該函式接受兩個字串作為引數,並根據是否找到子字串返回布林值。如果函式返回的值為 true,則找到子字串,反之亦然。
func Index(s, substr string) int
index() 函式存在於 strings 包中,用於獲取給定子字串第一次出現的索引。該函式接受兩個值作為引數。一個是字串,另一個是要檢測其出現的子字串。然後,該函式以整數格式返回該子字串的第一次出現。
演算法
步驟 1 - 首先,我們需要匯入 fmt 包。
步驟 2 - 現在,開始 main() 函式。在這裡初始化一個字串和子字串,併為其賦值。
步驟 3 - 現在,檢查 contains() 函式返回的值是否為 true,並根據需要列印結果。
示例 1
在此示例中,我們將使用 strings 包中提供的 contain() 函式來檢查字串是否包含指定的子字串。
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println("Example 1:")
var str string = "Live young live free"
var substr string = "young"
if strings.Contains(str, substr) {
fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
} else {
fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
}
fmt.Println()
fmt.Println("Example 2:")
substr = "and"
if strings.Contains(str, substr) {
fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
} else {
fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
}
}
輸出
Example 1: The string 'Live young live free' contains the substring 'young'. Example 2: The string 'Live young live free' does not contain the substring 'and'.
示例 2
在此示例中,我們將使用 strings 包中提供的 index() 函式來檢查字串是否包含指定的子字串。
package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println("Example 1:")
var str string = "Live young live free"
var substr string = "live"
if strings.Index(str, substr) != -1 {
fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
} else {
fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
}
fmt.Println()
fmt.Println("Example 2:")
substr = "either"
if strings.Index(str, substr) != -1 {
fmt.Printf("The string '%s' contains the substring '%s'.\n", str, substr)
} else {
fmt.Printf("The string '%s' does not contain the substring '%s'.\n", str, substr)
}
}
輸出
Example 1: The string 'Live young live free' contains the substring 'live'. Example 2: The string 'Live young live free' does not contain the substring 'either'.
結論
我們已經成功編譯並執行了一個 Go 語言程式來檢查字串是否包含指定的子字串以及示例。我們在本文中實現了三個程式,在第一個程式中,我們只是使用 for 迴圈來實現結果,而在第二個和第三個示例中,我們分別使用 contains() 和 index() 內建函式來執行相應的操作。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP