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
+8k Golang : Get local time and equivalent time in different time zone
+14.6k Golang : Convert PNG transparent background image to JPG or JPEG image
+3.6k Javascript : How to replace HTML inside <div>?
+9.6k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+9.5k Golang : Find network of an IP address
+4.1k Golang : Warp text string by number of characters or runes example
+4.6k Android Studio : How to detect camera, activate and capture example
+6.7k Golang : Check if user agent is a robot or crawler example
+5.8k Golang : Temperatures conversion example
+14.2k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+7.3k Golang : Convert(cast) string to int64
+4.7k Golang : Get expvar(export variables) to work with multiplexer