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
+11.5k Golang : Characters limiter example
+5.5k Golang : Intercept, inject and replay HTTP traffics from web server
+15.7k Golang : rune literal not terminated error
+15.1k Golang : Basic authentication with .htpasswd file
+5.8k Golang : Detect words using using consecutive letters in a given string
+20k Golang : Append content to a file
+8.3k Golang : Randomize letters from a string example
+10.7k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+11.9k Golang : Secure file deletion with wipe example
+5.1k Nginx and PageSpeed build from source CentOS example
+6.4k Golang & Javascript : How to save cropped image to file on server