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
+22.6k Golang : Calculate time different
+11.5k Golang : Verify Linux user password again before executing a program example
+5.9k Golang : Scan forex opportunities by Bollinger bands
+7.6k Golang : Get today's weekday name and calculate target day distance example
+5.7k PHP : Get client IP address
+13k Golang : Date and Time formatting
+12.5k Golang : http.Get example
+27k Golang : dial tcp: too many colons in address
+7.1k Golang : alternative to os.Exit() function
+5.3k PHP : Fix Call to undefined function curl_init() error
+21.1k Golang : Encrypt and decrypt data with TripleDES
+8.6k Golang : HTTP Routing with Goji example