English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Golang Basic Tutorial

Golang Control Statement

Golang Function & Method

Golang Struct

Golang Slice & Array

Golang String(String)

Golang Pointer

Golang Interface

Golang Concurrency

Golang Exception(Error)

Golang Miscellaneous

Go judge whether the string contains the specified character

In Go strings, you can use the given function to check for the existence of a specified character in a string. These functions are defined under the string package, so you must import the string package in your program to access these functions:

1.Contains:This function is used to check if a given character exists in the specified string. If the character is found in the given string, it will return true; otherwise, it will return false.

Syntax:

func Contains(str, chstr string) bool

here,stris the original string, and}}chstris the string you want to check. Let's discuss this concept with an example:

//exists in the string
//The specified string
package main
import (
    "fmt"
    "strings"
)
func main() {
    //Create and initialize a string
    str1 := "Welcome to w3codebox for w3codebox "
    str2 := "Here! we learn about go strings"
    fmt.Println("Original string")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
    //to check if a string exists
    //Using the Contains() function
    res1 := strings.Contains(str1, "w3codebox")
    res2 := strings.Contains(str2, "GFG")
    //Display result
    fmt.Println("\nResult 1: ", res1)
    fmt.Println("Result 2: ", res2)
}

Output:

Original string
String 1:  Welcome to w3codebox for w3codebox
String 2:  Here! we learn about go strings
Result 1:  true
Result 2:  false

 
2. ContainsAny:This function is used to check if any Unicode character from charstr exists in the given string (str). If any Unicode character from charstr exists in the given string (str), this method returns true; otherwise, it returns false.

Syntax:

func ContainsAny(str, charstr string) bool

here,str is the original string,charstr is a Unicode character in chars. Let's discuss this concept with an example:

//Whether a string contains or does not contain a specified substring
package main
import (
    "fmt"
    "strings"
)
func main() {
    //Create and initialize a string
    str1 := "Welcome to Geeks for Geeks"
    str2 := "Here! we learn about go strings"
    //to check if a string exists
    //Using the ContainsAny() function
    res1 := strings.ContainsAny(str1, "Geeks")
    res2 := strings.ContainsAny(str2, "GFG")
    res3 := strings.ContainsAny("w3codebox
    res4 := strings.ContainsAny("w3codebox
    res5 := strings.ContainsAny(" ", " ")
    res6 := strings.ContainsAny("w3codebox
    //Display result
    fmt.Println("\nResult 1: ", res1)
    fmt.Println("Result 2: ", res2)
    fmt.Println("Result 3: ", res3)
    fmt.Println("Result 4: ", res4)
    fmt.Println("Result 5: ", res5)
    fmt.Println("Result 6: ", res6)
}

Output:

Result 1:  true
Result 2:  false
Result 3:  false
Result 4:  false
Result 5:  true
Result 6:  false