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
+5k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+8.8k Golang : Test a slice of integers for odd and even numbers
+7.5k Golang : Another camera capture GUI application with GTK and OpenCV
+4.4k Javascript : How to loop over and parse JSON data?
+7.7k Golang : Find duplicate files with filepath.Walk
+10.5k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+9.5k Swift : Convert (cast) String to Integer
+4.4k Unix/Linux : How to archive and compress entire directory ?
+7.9k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+8.4k Facebook : Getting the friends list with PHP return JSON format
+9.8k Golang : How to determine a prime number?