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.3k Golang : Check if element exist in map
+4.7k Nginx and PageSpeed build from source CentOS example
+27.8k Golang : Connect to database (MySQL/MariaDB) server
+6.7k Golang : Fibonacci number generator examples
+6.7k Golang : Normalize email to prevent multiple signups example
+5.9k Java : Human readable password generator
+8.3k Linux/Unix : fatal: the Postfix mail system is already running
+7.2k Golang : Create zip/ePub file without compression(use Store algorithm)
+8.8k Golang : Simple histogram example
+35.6k Golang : Save image to PNG, JPEG or GIF format.
+6.4k Golang : Warp text string by number of characters or runes example