How to check if a character exists in a string with Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
)

func main() {
    // Define the string we want to search in
    s := "golang"

    // Check for a rune using ContainsRune
    posG := strings.ContainsRune(s, 'g') // true
    posZ := strings.ContainsRune(s, 'z') // false

    // Print the raw boolean results
    fmt.Println(posG)
    fmt.Println(posZ)

    // Conditional check
    if strings.ContainsRune(s, 'g') {
        fmt.Println("exists")
    } else {
        fmt.Println("not exists")
    }
}


/*
run:

true
false
exists

*/

 



answered 10 hours ago by avibootz
edited 10 hours ago by avibootz
...