Go: What's the best way to detect invalid JSON string characters? -
what's best, efficient way detect whether go string contains characters invalid in json strings? in other words, what's go equivalent answer java question? use strings.containsany (assuming ascii control characters)?
ctlchars := string([]byte{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 127, }) if strings.containsany(str, ctlchars) { println("has control chars") }
if looking identify control characters (as in answers java question pointed to), might want use unicode.iscontrol
simpler solution.
https://golang.org/pkg/unicode/#iscontrol
func containscontrolchar(s string) bool { _, c := range s { if unicode.iscontrol(c) { return true } } return false }
playground: https://play.golang.org/p/pr_9mmt-th
Comments
Post a Comment