C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Result: The tab, newline and space characters are detected by the unicode.IsSpace func.
Golang program that uses unicode.IsSpace
package main
import (
"fmt"
"unicode"
)
func main() {
value := "\tHello, friends\n"
// Loop over chars in string.
for _, v := range value {
// Test each character to see if it is whitespace.
if unicode.IsSpace(v) {
fmt.Println("Is space:", v)
if v == 32 {
fmt.Println("ASCII code for space")
}
}
}
}
Output
Is space: 9
Is space: 32
ASCII code for space
Is space: 10
Tip: This style of code is cleaner and less prone to bugs than implementing a custom IsSpace method ourselves.
Golang program that uses TrimFunc, unicode.IsSpace
package main
import (
"fmt"
"unicode"
"strings"
)
func main() {
value := "\n Hello, friends"
// Use unicode.IsSpace as the func argument to TrimFunc.
result := strings.TrimFunc(value, unicode.IsSpace)
// Print before and after.
fmt.Println("[" + value + "]")
fmt.Println("[" + result + "]")
}
Output
[
Hello, friends]
[Hello, friends]