Golang : How to check if a string starts or ends with certain characters or words?
Problem :
You are building a rule for parsing text strings and you want to determine if a string starts or ends with certain words or characters. How to do that?
Solution :
Use strings.HasPrefix()
function to check if a string starts with certain word.
For example,
if strings.HasPrefix(input, "please") {
return "You don't have to be so polite"
}
to check if the strings ends with certain character or word. Use strings.HasSuffix()
function.
References :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+12k Golang : Clean formatting/indenting or pretty print JSON result
+13.6k Golang : Qt progress dialog example
+11k Golang : Replace a parameter's value inside a configuration file example
+12.6k Golang : flag provided but not defined error
+10.1k Golang : Compare files modify date example
+17.9k Golang : How to make a file read only and set it to writable again?
+11.7k Golang : Concurrency and goroutine example
+9.9k Golang : Sort and reverse sort a slice of integers
+7.5k Golang : Get YouTube playlist
+6.2k PHP : Get client IP address
+8.2k Golang : Qt splash screen with delay example