Golang : How to detect if a sentence ends with a punctuation?
Problem : I need to detect if a line ends with a punctuation mark or not. How to do that?
Solution : In Golang, a string is also a slice, get the last character out from the sentence, convert the string to rune and use unicode.IsPunct()
function to check if the last character is a punctuation or not.
Here you go!
package main
import (
"log"
"unicode"
)
func main() {
//str := "this is a line with fullstop."
str := "this is a line with without fullstop"
lastChar := str[len(str)-1:]
lastCharRune := []rune(lastChar)
if unicode.IsPunct(lastCharRune[0]) { // this is how to convert string to rune ;-)
log.Println("lastChar is a punctuation :", lastChar)
} else {
log.Println("lastChar is not a punctuation :", lastChar)
}
}
Happy coding!
See also : Golang : Use NLP to get sentences for each paragraph example
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
+6.3k PHP : Get client IP address
+15.8k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+9.3k Golang : Write multiple lines or divide string into multiple lines
+19.4k Golang : Display list of time zones with GMT
+7.2k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+25.9k Golang : Daemonizing a simple web server process example
+6.4k Javascript : Generate random key with specific length
+10.7k Golang : Interfacing with PayPal's IPN(Instant Payment Notification) example
+8.5k Golang : Generate Datamatrix barcode
+6.9k Android Studio : Hello World example
+9.1k Golang : Handle sub domain with Gin
+24k Golang : Fix type interface{} has no field or no methods and type assertions example