Contains()

manikanta528's avatar
Published Jul 4, 2023
Contribute to Docs

The Contains() function returns a boolean value indicating whether a given substring is present or not in a given string. The method returns true if the substring is present, otherwise it returns false.

Syntax

strings.Contains(str, sub_str)

Where str is the original string and sub_str is the substring to check against the original string.

Example

The following example demonstrates the use of the strings.Contains() function.

package main
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Contains("folder","old"))
fmt.Println(strings.Contains("folder","code"))
}

The output will be:

true
false

Codebyte Example

The following example is runnable and uses the strings.Contains() function to determine if the given value is present in the string codecademy.

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Go on Codecademy