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
+12.9k Golang : error parsing regexp: invalid or unsupported Perl syntax
+9.5k Golang : Function wrapper that takes arguments and return result example
+8.7k Golang : Gonum standard normal random numbers example
+21.7k Golang : Securing password with salt
+25.3k Golang : How to write CSV data to file
+8.5k Golang : How to capture return values from goroutines?
+11.3k How to tell if a binary(executable) file or web application is built with Golang?
+6.4k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+12k Golang : Search and extract certain XML data example
+11.7k Golang : Simple client-server HMAC authentication without SSL example
+6k PHP : Proper way to get UTF-8 character or string length
+7.2k Golang : Command line ticker to show work in progress