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
+6.3k CodeIgniter : form input set_value cause " to become & quot
+11.8k Golang : How to parse plain email text and process email header?
+20.7k Golang : Underscore or snake_case to camel case example
+6.2k WARNING: UNPROTECTED PRIVATE KEY FILE! error message
+11.4k Golang : Display a text file line by line with line number example
+39k Golang : How to read CSV file
+5.2k Unix/Linux : How to archive and compress entire directory ?
+20.9k Golang : Sort and reverse sort a slice of strings
+13.7k Golang : Gin framework accept query string by post request example
+22.8k Golang : Test file read write permission example
+6.6k Golang : Check if password length meet the requirement
+12.5k Golang : Sort and reverse sort a slice of bytes