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.1k Golang : Save(pipe) HTTP response into a file
+6.7k Golang : Get expvar(export variables) to work with multiplexer
+10k Golang : Get login name from environment and prompt for password
+9.6k Golang : Copy map(hash table) example
+12.1k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+13.9k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+17.6k Golang : delete and modify XML file content
+6.3k PHP : Proper way to get UTF-8 character or string length
+12.9k Swift : Convert (cast) Int to String ?
+20.1k Golang : Convert seconds to human readable time format example
+11k Golang : Roll the dice example
+10.6k Golang : Resolve domain name to IP4 and IP6 addresses.