Golang : Removes punctuation or defined delimiter from the user's input
Problem :
You need to clean up your user's input from punctuation or certain delimiter defined by you. For example, you need to parse the user's input, but only want to deal with words and certain symbols for tokenizing purpose. How to do that?
Solution :
First, we will write a function to test if the single character is a punctuation or defined delimiter.
package main
import (
"fmt"
"strings"
)
const delim = "?!.;,*"
func isDelim(c string) bool {
if strings.Contains(delim, c) {
return true
}
return false
}
func main() {
char := "i"
fmt.Printf("Is [%v] a delim : %v\n", char, isDelim(char))
char = ","
fmt.Printf("Is [%v] a delim : %v\n", char, isDelim(char))
char = ";"
fmt.Printf("Is [%v] a delim : %v\n", char, isDelim(char))
char = "s"
fmt.Printf("Is [%v] a delim : %v\n", char, isDelim(char))
}
Output :
Is [i] a delim : false
Is [,] a delim : true
Is [;] a delim : true
Is [s] a delim : false
Then, iterate each of the characters in a string to test out if the character is a punctuation or delimiter defined by you.
package main
import (
"fmt"
"strings"
)
const delim = "?!.;,*"
func isDelim(c string) bool {
if strings.Contains(delim, c) {
return true
}
return false
}
func cleanString(input string) string {
size := len(input)
temp := ""
var prevChar string
for i := 0; i < size; i++ {
//fmt.Println(input[i])
str := string(input[i]) // convert to string for easier operation
if (str == " " && prevChar != " ") || !isDelim(str) {
temp += str
prevChar = str
} else if prevChar != " " && isDelim(str) {
temp += " "
}
}
return temp
}
func main() {
str := "H ello! Who are you?!, What are you?"
newStr := cleanString(str)
fmt.Println("Original : ", str)
fmt.Println("Cleaned : ", newStr)
}
Output :
Original : H ello! Who are you?!, What are you?
Cleaned : H ello Who are you What are you
Reference :
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.9k CodeIgniter : "Fatal error: Cannot use object of type stdClass as array" message
+15.3k Golang : How to convert(cast) IP address to string?
+12.8k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+26k Golang : Calculate future date with time.Add() function
+8.3k Linux/Unix : fatal: the Postfix mail system is already running
+27.3k PHP : Count number of JSON items/objects
+8.2k PHP : How to parse ElasticSearch JSON ?
+11.5k Golang : GTK Input dialog box examples
+31.2k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+5.8k PHP : How to check if an array is empty ?
+11.3k Golang : Fuzzy string search or approximate string matching example