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
+10.8k Golang : Fix go.exe is not compatible with the version of Windows you're running
+21.9k Golang : Convert seconds to minutes and remainder seconds
+10.5k Golang : Simple File Server
+17.8k Golang : Convert IPv4 address to decimal number(base 10) or integer
+5.4k Unix/Linux : How to find out the hard disk size?
+4.8k Google : Block or disable caching of your website content
+13.7k Golang : Compress and decompress file with compress/flate example
+8k Golang : Configure Apache and NGINX to access your Go service example
+17.4k Golang : Upload/Receive file progress indicator
+19.6k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+13.9k Golang : Chunk split or divide a string into smaller chunk example
+7.6k Swift : Convert (cast) String to Double