字符串.TrimFunc() Golang 中的函数用于返回字符串s 的切片,其中删除了满足 f(c) 的所有前导和尾随 Unicode 代码点 c。
句法:
func TrimFunc(s string, f func(rune) bool) string
这里,s 是字符串,函数检查 rune 的值,如果可以修剪则返回 true。
返回类型:它从字符串除去指定的字符后返回字符串。
示例 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)
    }))
}
输出:
GeeksForGeeks
示例 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)
    }))
}
输出:
1234GeeksForGeeks
