How to check if a string starts with specific substring in Go

2 Answers

0 votes
package main

import (
    "fmt"
    "strings"
)

func main() {

    s := "go java php python"

    if strings.HasPrefix(s, "go") {
        fmt.Println("yes")
    } else {
        fmt.Println("no")
    }
}
 
 
/*
run:
 
yes
 
*/

 



answered Aug 7, 2020 by avibootz
0 votes
package main

import (
	"fmt"
	"strings"
)

func main() {
	var s string = "go java php python"
	var substring string = "go"
	var match bool

	match = strings.Index(s, substring) == 0

	if match {
		fmt.Println("yes")
	} else {
		fmt.Println("no")
	}
}


/*
run:

yes

*/

 



answered Aug 2, 2024 by avibootz

Related questions

2 answers 208 views
1 answer 161 views
1 answer 164 views
1 answer 119 views
1 answer 137 views
1 answer 152 views
...