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() 内置函数来执行相应的操作。

更新于: 2023年2月13日

5K+ 阅读量

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.