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
+13.7k Golang : Search folders for file recursively with wildcard support
+5.6k Golang : Totalize or add-up an array or slice example
+7.7k Golang : Set or add headers for many or different handlers
+19.9k Golang : Sort and reverse sort a slice of strings
+10.7k Golang : Calculations using complex numbers example
+8.1k Golang : Go as a script or running go with shebang/hashbang style
+8k Golang : automatically figure out array length(size) with three dots
+4.2k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+6.2k Golang : Check if one string(rune) is permutation of another string(rune)
+9.6k Golang : How to transmit update file to client by HTTP request example
+24.1k Golang : Convert IP address string to long ( unsigned 32-bit integer )
+12.7k Golang : How to check if a file is hidden?