📜  字符串.TrimRightFunc() Golang函数示例

📅  最后修改于: 2021-10-25 02:12:39             🧑  作者: Mango

字符串.TrimRightFunc()函数返回字符串s 的一个切片,其中删除了所有满足 f(c) 的尾随 Unicode 代码点 c。

句法:

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

这里,s 是字符串,func() 是满足字符串字符的方法。

返回值:从字符串删除尾随字符后返回字符串。

示例 1:

// Golang program to illustrate
// the strings.TrimRightFunc() 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 only from Right
    fmt.Print(strings.TrimRightFunc("77GeeksForGeeks!!!", func(r rune) bool {
        return !unicode.IsLetter(r)
    }))
}

输出:

77GeeksForGeeks

示例 2:

// Golang program to illustrate
// the strings.TrimRightFunc() 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 only from left
    fmt.Print(strings.TrimRightFunc("!!!34GeeksForGeeks!!!!", func(r rune) bool {
        return !unicode.IsLetter(r) && !unicode.IsNumber(r)
    }))
}

输出:

!!!34GeeksForGeeks