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
+5.7k Get Facebook friends working in same company
+15k Golang : Get IP addresses of a domain name
+18.1k Golang : How to get own program name during runtime ?
+4.9k Golang : Launching your executable inside a console under Linux
+5.2k Golang : Create new color from command line parameters
+12.7k Golang : Gin framework accept query string by post request example
+17.5k Unmarshal/Load CSV record into struct in Go
+6.3k Golang : Process json data with Jason package
+9.5k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+5.1k PHP : How to check if an array is empty ?
+6.3k Golang : Dealing with postal or zip code example
+4.3k PHP : See installed compiled-in-modules