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
+10.6k Golang : Clean formatting/indenting or pretty print JSON result
+4.1k Unix/Linux : secure copying between servers with SCP command examples
+8.3k Golang : Launch Mac OS X Preview (or other OS) application from your program example
+17.2k Golang : Example for RSA package functions
+6.5k Golang : What fmt.Println() can do and println() cannot do
+6.9k Swift : Convert (cast) String to Float
+5.1k Golang : How to solve "too many .rsrc sections" error?
+21k Golang : untar or extract tar ball archive example
+12.7k Golang : Get current time
+9.3k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+6.3k Golang : Lock executable to a specific machine with unique hash of the machine