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
+7.3k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+12.7k Golang : Convert IPv4 address to packed 32-bit binary format
+10.1k Golang : Wait and sync.WaitGroup example
+9.1k Golang : Timeout example
+17.7k Golang : Get all upper case or lower case characters from string example
+5.5k Swift : Get substring with rangeOfString() function example
+16.3k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error
+15.8k Golang : Get sub string example
+5.6k Golang : Fix opencv.LoadHaarClassifierCascade The node does not represent a user object error
+13.8k Golang : concatenate(combine) strings
+13.4k Golang : Query string with space symbol %20 in between
+12.9k Golang : Convert(cast) int to int64