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
+8.6k Linux/Unix : fatal: the Postfix mail system is already running
+6k AWS S3 : Prevent Hotlinking policy
+36.7k Golang : Validate IP address
+18.8k Golang : convert int to string
+9.9k Golang : Detect number of active displays and the display's resolution
+18.8k Golang : Iterating Elements Over A List
+10.7k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+13.7k Golang : Query string with space symbol %20 in between
+13.5k Golang : Linear algebra and matrix calculation example
+14.1k Golang : convert rune to unicode hexadecimal value and back to rune character
+6.5k CodeIgniter : form input set_value cause " to become & quot
+12.4k Golang : Print UTF-8 fonts on image example