Open In App

Check if the given characters is present in Golang String

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. In Go strings, you are allowed to check the given characters present in the string using the given functions. These functions are defined under strings package, so you have to import strings package in your program to access these functions:

1. Contains: This function is used to check the given letters present in the given string or not. If the letter is present in the given string, then it will return true, otherwise, return false.

Syntax:

func Contains(str, chstr string) bool

Here, str is the original string and chstr is the string which you wants to check. Let us discuss this concept with the help of an example:

Example:




// Go program to illustrate how to check
// the string is present or not in the
// specified string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating and initializing strings
    str1 := "Welcome to Geeks for Geeks"
    str2 := "Here! we learn about go strings"
  
    fmt.Println("Original strings")
    fmt.Println("String 1: ", str1)
    fmt.Println("String 2: ", str2)
  
    // Checking the string present or not
    //  Using Contains() function
    res1 := strings.Contains(str1, "Geeks")
    res2 := strings.Contains(str2, "GFG")
  
    // Displaying the result
    fmt.Println("\nResult 1: ", res1)
    fmt.Println("Result 2: ", res2)
  
}


Output:

Original strings
String 1:  Welcome to Geeks for Geeks
String 2:  Here! we learn about go strings

Result 1:  true
Result 2:  false

 
2. ContainsAny: This function is used to check whether any Unicode code points in chars are present in the given string. If any Unicode code points in chars are available in the given string, then this method returns true, otherwise, return false.

Syntax:

func ContainsAny(str, charstr string) bool

Here, str is the original string and charstr is the Unicode code points in chars. Let us discuss this concept with the help of an example:

Example:




// Go program to illustrate how to check the
// string is present or not in the specified
// string
package main
  
import (
    "fmt"
    "strings"
)
  
// Main function
func main() {
  
    // Creating and initializing strings
    str1 := "Welcome to Geeks for Geeks"
    str2 := "Here! we learn about go strings"
  
    // Checking the string present or not
    // Using ContainsAny() function
    res1 := strings.ContainsAny(str1, "Geeks")
    res2 := strings.ContainsAny(str2, "GFG")
    res3 := strings.ContainsAny("GeeksforGeeks", "G & f")
    res4 := strings.ContainsAny("GeeksforGeeks", "u | e")
    res5 := strings.ContainsAny(" ", " ")
    res6 := strings.ContainsAny("GeeksforGeeks", " ")
  
    // Displaying the 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:  true
Result 4:  true
Result 5:  true
Result 6:  false


Last Updated : 26 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads