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
+12.4k Golang : Parsing or breaking down URL
+14.1k Golang : File path independent of Operating System
+19.6k Golang : How to reverse slice or array elements order
+10k Golang : Arithmetic operation with numerical slices or arrays example
+4.1k Golang : How to solve "too many .rsrc sections" error?
+7.6k Golang : Eroding and dilating image with OpenCV example
+12.6k Golang : Basic authentication with .htpasswd file
+5.5k Golang : Dealing with postal or zip code example
+5.9k Golang : Sort words with first uppercase letter
+4.4k Fontello : How to load and use fonts?
+7.8k Golang : How to protect your source code from client, hosting company or hacker?
+10.6k Golang : Covert map/slice/array to JSON or XML format