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
+9.4k Golang : Create unique title slugs example
+17.9k Golang : Defer function inside init()
+10k Golang : Turn string or text file into slice example
+15.3k Golang : Get query string value on a POST request
+5.4k Javascript : How to loop over and parse JSON data?
+12.8k Golang : zlib compress file example
+22.8k Golang : untar or extract tar ball archive example
+10.9k Golang : Sieve of Eratosthenes algorithm
+13.6k Golang : Strings comparison
+8.9k Golang : GMail API create and send draft with simple upload attachment example
+13.9k Golang : Get current time
+16.8k Golang : Get own process identifier