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
+15.9k Golang : XML to JSON example
+10.1k How to test Facebook App on localhost ?
+7.9k Golang : Get curl -I or head data from URL example
+11.3k Golang : Extract part of string with regular expression
+13k Golang : syscall.Socket example
+5.3k Golang : Extract sub-strings
+8.5k Golang : Convert(cast) string to int64
+6.4k Android Studio : How to detect camera, activate and capture example
+26.9k Golang : Read, Write(Create) and Delete Cookie example
+29.1k Golang : Calculate percentage change of two values
+8.8k Golang : Identifying Golang HTTP client request
+7.7k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?