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
+23.4k Golang : Fix type interface{} has no field or no methods and type assertions example
+7.2k Golang : How to handle file size larger than available memory panic issue
+7k Golang : File system scanning
+5k Javascript : Change page title to get viewer attention
+6.7k Golang : Takes a plural word and makes it singular
+6.3k Golang : Combine slices of complex numbers and operation example
+18.1k Golang : Aligning strings to right, left and center with fill example
+24.9k Golang : Convert uint value to string type
+14.1k Golang : Send email with attachment(RFC2822) using Gmail API example
+21.8k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+23.6k Golang : Use regular expression to validate domain name
+6.1k Golang : Selection sort example