Golang Tutorial
Fundamentals
Control Statements
Functions & Methods
Structure
Arrays & Slices
String
Pointers
Interfaces
Concurrency
Checking if given characters are present in a string is a fundamental task in many programming scenarios. Let's see how you can achieve this in Go.
strings.Contains
The simplest way to check if a substring is present within a string is by using the Contains
function from the strings
package.
package main import ( "fmt" "strings" ) func main() { str := "Hello, Gophers!" // Check if the string contains "Goph" if strings.Contains(str, "Goph") { fmt.Println("String contains 'Goph'") } else { fmt.Println("String does not contain 'Goph'") } }
If you have multiple characters and you want to check if any of them are present in the string, you can iterate over them and use the strings.ContainsRune
function.
package main import ( "fmt" "strings" ) func main() { str := "Hello, Gophers!" chars := []rune{'a', 'e', 'i', 'o', 'u'} // checking for vowels for _, char := range chars { if strings.ContainsRune(str, char) { fmt.Printf("String contains the character '%c'\n", char) } } }
strings.Index
Another way to check if a substring or character is present in a string is by using the Index
or IndexRune
function from the strings
package. These functions return the start index of the first instance of the substring/character in the string, or -1
if the substring/character is not present.
package main import ( "fmt" "strings" ) func main() { str := "Hello, Gophers!" substr := "Goph" if strings.Index(str, substr) != -1 { fmt.Printf("String contains '%s'\n", substr) } else { fmt.Printf("String does not contain '%s'\n", substr) } }
These methods should help you determine whether specific characters or substrings are present in a given string in Go. Depending on the use-case, you can choose the most suitable function for your needs.
Golang Check if Character is in String:
package main import "fmt" func charInString(str string, char rune) bool { for _, c := range str { if c == char { return true } } return false } func main() { text := "Hello, Golang!" charToCheck := 'o' result := charInString(text, charToCheck) fmt.Println(result) // Output: true }
String Contains Character Golang:
strings
package to check if a character is present in the string.package main import ( "fmt" "strings" ) func main() { text := "Golang" charToCheck := 'o' // Using strings.Contains result := strings.ContainsRune(text, charToCheck) fmt.Println(result) // Output: true }
Golang Check if Substring Exists in String:
strings.Contains
function to check if a substring is present in a string.package main import "fmt" func main() { text := "Hello, Golang!" substring := "Golang" result := strings.Contains(text, substring) fmt.Println(result) // Output: true }
How to Find if a Character is in a Golang String:
// Refer to the example in point 1.
Check if All Characters in a List are in a Golang String:
package main import "fmt" func allCharsInString(str string, chars []rune) bool { for _, char := range chars { if !charInString(str, char) { return false } } return true } func main() { text := "Hello, Golang!" charsToCheck := []rune{'H', 'o', 'l'} result := allCharsInString(text, charsToCheck) fmt.Println(result) // Output: true }
Golang String Contains Any of Multiple Characters:
package main import "fmt" func anyCharInString(str string, chars []rune) bool { for _, char := range chars { if charInString(str, char) { return true } } return false } func main() { text := "Hello, Golang!" charsToCheck := []rune{'x', 'y', 'o'} result := anyCharInString(text, charsToCheck) fmt.Println(result) // Output: true }
Using strings.Contains in Golang:
strings.Contains
function checks if one string contains another string.package main import "fmt" func main() { text := "Hello, Golang!" substring := "Golang" result := strings.Contains(text, substring) fmt.Println(result) // Output: true }
Golang Check if String Contains Only Specific Characters:
package main import "fmt" func onlyAllowedChars(str string, allowedChars []rune) bool { for _, char := range str { if !charInString(string(allowedChars), char) { return false } } return true } func main() { text := "Go123" allowedChars := []rune{'G', 'o', '1', '2', '3'} result := onlyAllowedChars(text, allowedChars) fmt.Println(result) // Output: true }
Efficient Ways to Check Character Presence in Golang String:
package main import "fmt" func efficientCharInString(str string, char rune) bool { charSet := make(map[rune]bool) for _, c := range str { charSet[c] = true } return charSet[char] } func main() { text := "Hello, Golang!" charToCheck := 'o' result := efficientCharInString(text, charToCheck) fmt.Println(result) // Output: true }
Golang Rune and String Comparison for Character Presence:
package main import "fmt" func runeInString(str string, char rune) bool { for _, c := range str { if c == char { return true } } return false } func main() { text := "Hello, Golang!" charToCheck := 'o' result := runeInString(text, charToCheck) fmt.Println(result) // Output: true }