當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Golang strings.TrimFunc()用法及代碼示例

strings.TrimFunc() Golang中的函數用於返回字符串s的一部分,其中所有滿足f(c)的前導和尾隨Unicode代碼點c均被刪除。

用法:

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

s是字符串,函數檢查符文的值,如果可以修剪,則返回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



相關用法


注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 strings.TrimFunc() Function in Golang With Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。