Golang 用于检查字符串是否包含指定的子字符串

Golang 用于检查字符串是否包含指定的子字符串

字符串和子字符串是什么

在Go中,子字符串是较大字符串的一部分。它是通过提供起始索引和长度来指定的,并且包含从起始索引开始并延伸到指定长度的原始字符串的字符。子字符串中的字符仍然是原始字符串的一部分,并且共享同一块内存。

在Go中,字符串是字符的序列。它是一个不可变的数据类型,意味着一旦创建了一个字符串,就无法修改它。字符串用双引号(“”)括起来,可以包含任何字母、数字和符号的组合。

在本文中,我们将使用在Go语言中的循环和库函数来实现这个结果。子字符串可以被视为完整字符串的子集。子字符串的元素始终在原始字符串中。

方法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

The index() 函数位于strings包中,用于获取给定子字符串首次出现的索引。该函数接受两个值作为参数。一个是字符串,另一个是要检测的子字符串。函数以整数格式返回该子字符串的首次出现位置。

步骤

  • 步骤1 - 首先,我们需要导入fmt包。

  • 步骤2 - 现在,开始main()函数。在这里初始化一个字符串和子字符串,并为其赋值。

  • 步骤3 - 现在,检查contains()函数返回的值是否为true,根据结果打印结果。

示例1

在这个示例中,我们将使用strings包中的contains()函数来检查一个字符串是否包含指定的子字符串。

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

在这个示例中,我们将使用字符串包中的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'.

总结

我们成功地编译并执行了一个 Golang 程序,用于检查字符串是否包含指定的子字符串,并提供了示例。在本文中,我们实现了三个程序。在第一个程序中,我们简单地使用 for 循环来实现结果。而在第二个和第三个示例中,我们分别使用了 contains() 和 index() 内置函数来进行相应的操作。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程