strings.TrimSpace removes character of new line

5,213 views
Skip to first unread message

Archos

unread,
Oct 4, 2010, 4:59:31 AM10/4/10
to golang-nuts
It could be that new line character is defined in Unicode as a white
space.

* * *
package main

import (
"fmt"
"strings"
)

func main() {
l := "\tstairway to heaven \n"
fmt.Println("^" + l + "$")
fmt.Println("^" + strings.TrimSpace(l) + "$")
}
* * *

Andrew Gerrand

unread,
Oct 4, 2010, 5:02:16 AM10/4/10
to Archos, golang-nuts
On 4 October 2010 19:59, Archos <raul...@sent.com> wrote:
> It could be that new line character is defined in Unicode as a white
> space.

Yes, a newline is considered white space.

Andrew

peterGo

unread,
Oct 4, 2010, 8:21:42 AM10/4/10
to golang-nuts
Archos,

Consider the Go package source code as part of the documentation.

strings func TrimSpace
http://golang.org/pkg/strings/#TrimSpace
http://golang.org/src/pkg/strings/strings.go#L497

return TrimFunc(s, unicode.IsSpace)

unicode func IsSpace
http://golang.org/pkg/unicode/#IsSpace
http://golang.org/src/pkg/unicode/letter.go#L130

switch rune {
case '\t', '\n', '\v', '\f', '\r', ' ', 0x85, 0xA0:
return true
}
return false

It's that easy.

Peter
Reply all
Reply to author
Forward
0 new messages