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
+7.6k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+5.5k Golang : Intercept, inject and replay HTTP traffics from web server
+14.1k Golang : How to check if a file is hidden?
+6.8k Golang : How to determine if request or crawl is from Google robots
+13.2k Golang : Handle or parse date string with Z suffix(RFC3339) example
+5.9k Javascript : How to replace HTML inside <div>?
+12.1k Golang : Convert a rune to unicode style string \u
+20.1k Golang : How to get time from unix nano example
+13.7k Golang : reCAPTCHA example
+5.6k Clean up Visual Studio For Mac installation failed disk full problem
+24.1k Golang : Fix type interface{} has no field or no methods and type assertions example
+15.6k Golang : invalid character ',' looking for beginning of value