Check if the given characters is present in Golang String


Presence of a specific character or group of characters in a string is a typical procedure when working with strings. Several methods for determining whether specific characters are present in a Golang string will be covered in this article.

Using the strings.Contains() Function

Using the built-in strings is the simplest approach to determine whether a character or substring is contained in a Golang string. the contains() function. A boolean value indicating whether the substring was discovered in the string is returned by this function, which requires two arguments: the string to search for and the substring to look for. Here is a code example −

Example

package main

import (
   "fmt"
   "strings"
)

func main() {
   str := "hello world"
   fmt.Println(strings.Contains(str, "o")) // true
   fmt.Println(strings.Contains(str, "z")) // false
}

Output

true
false

In this example, we employ the strings. To determine whether the string "o" is present in the "hello world" string, use the Contains() method. The "o" character is present in the string, hence the method returns true. The "z" character is another thing we check for, and since it's not in the string, it returns false.

Using the strings.Index() Function

Another way to check for the presence of a character or substring in a Golang string is to use the strings.Index() function. This function returns the index of the first occurrence of the substring in the string, or -1 if the substring is not found. Here is an example code −

Example

package main

import (
   "fmt"
   "strings"
)

func main() {
   str := "hello world"
   fmt.Println(strings.Index(str, "o")) // 4
   fmt.Println(strings.Index(str, "z")) // -1
}

Output

4
-1

In this example, we use the strings.Index() function to check if the string "o" is present in the "hello world" string. The function returns the index of the first occurrence of the "o" character, which is 4. We also check for the presence of the "z" character, which returns -1 because it is not present in the string.

Using a Loop

Another way to check for the presence of a character in a Golang string is to use a loop to iterate over each character in the string and compare it to the target character. Here is an example code −

Example

package main

import (
   "fmt"
)

func main() {
   str := "hello world"
   target := 'o'
   found := false

   for _, c := range str {
      if c == target {
         found = true
         break
      }
   }
   fmt.Println(found) // true
}

Output

true

In this example, the "hello world" string's characters are iterated over and their values are compared to the "o" character using a loop. If a match is discovered, we set the found variable to true and end the loop. Following the loop, we output the result of found, which is true because the string contains the "o" character.

Conclusion

In conclusion, a number of built-in functions and loops can be used to check for the presence of a character or substring in a Golang string. the cords. strings and contains(). The easiest and most effective technique to determine whether a substring is present in a string is to use the index() function; loops can be used for more intricate searches. You can develop more effective and efficient Golang code when working with strings if you are aware of these strategies.

Updated on: 07-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements