How to remove leading and trailing whitespaces from a string in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
)

func main() {
    s := "         go python java php c c++    "

    fmt.Println(len(s)) // Length before trimming

    s = strings.TrimSpace(s)

    fmt.Println(len(s)) // Length after trimming

    fmt.Println(s) 
}




/*
run:

37
24
go python java php c c++

*/

 



answered Jul 26, 2025 by avibootz

Related questions

1 answer 106 views
1 answer 102 views
1 answer 81 views
2 answers 101 views
1 answer 120 views
1 answer 163 views
...