strings.TrimFunc() Function in Golang With Examples

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:



filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
    }))
}

chevron_right


Output:

GeeksForGeeks

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
    }))
}

chevron_right


Output:

1234GeeksForGeeks



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.