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.2k Golang : Extract part of string with regular expression
+9.5k Golang : Fix go.exe is not compatible with the version of Windows you're running
+15.7k Golang : Get number of CPU cores
+9.6k Golang : Bubble sort example
+7.7k Golang : Gonum standard normal random numbers example
+24.2k Golang : How to read integer value from standard input ?
+4.9k Golang : Measure execution time for a function
+13.4k Golang : Save(pipe) HTTP response into a file
+5.6k Golang : Skip or discard items of non-interest when iterating example
+19.8k Golang : How to get time zone and load different time zone?
+5.5k Android Studio : Hello World example
+8.6k Golang : Turn string or text file into slice example