Skip to content
Related Articles

Related Articles

strings.TrimFunc() Function in Golang With Examples
  • Last Updated : 03 May, 2020

strings.TrimFunc() Function in Golang is used to returns a slice of the string s with all leading and trailing Unicode code points c satisfying f(c) removed.

Syntax:

func TrimFunc(s string, f func(rune) bool) string

Here, s is the string and function checks the value of rune and returns true if it can be trimmed.

Return Type: It returns the string after removing the specified characters from string.

Example 1:






// Golang program to illustrate
// the strings.TrimFunc() Function
package main
  
// importing fmt, unicode and strings
import (
    "fmt"
    "strings"
    "unicode"
)
  
// Calling Main method
func main() {
  
    // Here we have a string. The function
    // returns true for the letters
    // and all other will trim out
    // from the string
    fmt.Print(strings.TrimFunc("77GeeksForGeeks!!!", func(r rune) bool {
        return !unicode.IsLetter(r)
    }))
}

Output:

GeeksForGeeks

Example 2:




// Golang program to illustrate
// the strings.TrimFunc() Function
  
package main
  
// importing fmt, unicode and strings
import (
    "fmt"
    "strings"
    "unicode"
)
  
// Calling Main method
func main() {
  
    // Here we have a string. The function
    // returns true for the letters
    // and numbers as well
    // and all other will trim out
    // from the string
    fmt.Print(strings.TrimFunc("1234GeeksForGeeks!!!!", func(r rune) bool {
        return !unicode.IsLetter(r) && !unicode.IsNumber(r)
    }))
}

Output:

1234GeeksForGeeks
My Personal Notes arrow_drop_up
Recommended Articles
Page :