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
+31.2k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+6.8k Restart Apache or Nginx web server without password prompt
+7.6k Golang : Load DSA public key from file example
+7.9k Golang : Variadic function arguments sanity check example
+21.5k Golang : Convert string slice to struct and access with reflect example
+12.2k Elastic Search : Return all records (higher than default 10)
+30.1k Golang : How to redirect to new page with net/http?
+21.6k Golang : Use TLS version 1.2 and enforce server security configuration over client
+6.2k Grep : How to grep for strings inside binary data
+8.2k PHP : How to parse ElasticSearch JSON ?
+19.3k Golang : Example for DSA(Digital Signature Algorithm) package functions