I am trying to write a function to truncate strings with special characters in golang. One example is below
"H㐀〾▓朗퐭텟şüöžåйкл¤"
However I am doing it based on the number of characters allowed and cutting it in the middle. This results in data getting corrupted.
The result comes out like
H㐀〾▓朗퐭텟şüöžå�...
The �
should not be there. How do we detect these special characters and split it based on the length of these characters?
package main
import (
"fmt"
"regexp"
)
var reNameBlacklist = regexp.MustCompile(`(&|>|<|\/|:|
|)*`)
var maxFileNameLength = 30
// SanitizeName sanitizes user names in an email
func SanitizeName(name string, limit int) string {
result := name
reNameBlacklist.ReplaceAllString(result, "")
if len(result) > limit {
result = result[:limit] + "..."
}
return result
}
func main() {
str := "H㐀〾▓朗퐭텟şüöžåйкл¤"
fmt.Println(str)
strsan := SanitizeName(str, maxFileNameLength)
fmt.Println(strsan)
}