So after googling for more than one hour I decided to write it myself and I was astonished that it took me only 5 minutes and it compiled at the first try (Go is such a great language!):
package main
import (
"strings"
"unicode"
)
func main() {
s := "I am a string\n Containing tooo many spaces and \n new lines"
println("Before: " + s)
s = stringMinifier(s)
println("After: " + s)
println("----------\n" + strings.Replace(s, "am", "am not anymore", 1))
}
func stringMinifier(in string) (out string) {
white := false
for _, c := range in {
if unicode.IsSpace(c) {
if !white {
out = out + " "
}
white = true
} else {
out = out + string(c)
white = false
}
}
return
}
Output:
Before: I am a string
Containing tooo many spaces and
new lines
After: I am a string Containing tooo many spaces and new lines
----------
I am not anymore a string Containing tooo many spaces and new lines
Nice! I have found some html minifiers but none for plain UTF-8 text
ReplyDeleteThx for the tip !
ReplyDeleteYeah, awesome! Thanks
ReplyDeletegreat. I wish I has something like this in the standard library.
ReplyDeleteYeaaahhhh...
ReplyDeleteFinally i found this...
Thanks so much..