GoLang String - Contains()

In this tutorial, we will learn about whether the given substring is present in the actual string, in Go Contains() method used to find substring present.

In the Go Language Contains() method is used to check whether the given substring is present in the actual string.

let us explore the uses case with help of simple examples.

String Contains():

Contains() is used to check whether the given substring is present in the actual string.

If the substring is present in the given string, true is returned.

If the substring is not present in the given string, false is returned. It is case-sensitive.

Syntax:

strings.Contains(actual_string, sub_string) It takes two parameters.

  1. First parameter is the actual_string
  2. Second parameter is the sub_string

It is important to specify the "strings" package in import.

Example 1:

Let us consider the string - "Hello gkindex" and let's check for the substrings - "gk","Index","hello".

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "Hello gkindex"
 
    fmt.Println("String: ", actual_string)
    
    // Check for "gk"
     fmt.Println("Does gk present in the string? : ", strings.Contains(actual_string,"gk"))
     
      // Check for "Index"
     fmt.Println("Does Index present in the string? : ", strings.Contains(actual_string,"Index"))
     
      // Check for "hello"
     fmt.Println("Does hello present in the string? : ", strings.Contains(actual_string,"hello"))
}

Output:

CopiedCopy Code

String:  Hello gkindex
Does gk present in the string? :  true
Does Index present in the string? :  false
Does hello present in the string? :  false

Explanation for the above output:

  1. gk exists in the actual string. So true is returned.
  2. Index doesn't exists in the actual string. So false is returned.
  3. hello doesn't exists in the actual string. So false is returned.
  4. hello and Hello are not same. As Contains() is case-sensitive, false is returned.

Example 2:

Let us consider the string - "Welcome to GO" and let us check for the substrings - "come","to","go".

CopiedCopy Code

package main

import (
    "fmt"
"strings")

func main() {
  
  // Consider the string
    var actual_string = "Welcome to GO"
 
    fmt.Println("String: ", actual_string)
    
    // Check for "come"
     fmt.Println("Does come present in the string? : ", strings.Contains(actual_string,"come"))
     
      // Check for "to"
     fmt.Println("Does to present in the string? : ", strings.Contains(actual_string,"to"))
     
      // Check for "go"
     fmt.Println("Does go present in the string? : ", strings.Contains(actual_string,"go"))
}

Output:

CopiedCopy Code

String:  Welcome to GO
Does come present in the string? :  true
Does to present in the string? :  true
Does go present in the string? :  false

Explanation for the above output:

  1. come exists in the actual string. So true is returned.
  2. to exists in the actual string. So true is returned.
  3. GO does not exist in the actual string. So false is returned.
  4. go and GO are not same. As Contains() is case-sensitive, false is returned.

Conclusion

Now we know how to check if the given substring is present in the string or not using Contains() method in a Golang and understood the implementation with working examples.