golang strings 常用函数

这篇具有很好参考价值的文章主要介绍了golang strings 常用函数。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

官网
https://pkg.go.dev/strings

这里介绍最常用的函数
1、Fields 以空格为字段分割符,返回[]string

func strings.Split(s string, sep string) []string

2、Contains 是否包含子串,返回bool

 func strings.Contains(s string, substr string) bool
//也可以加!号,表示不包含
!strings.Contains(s string, substr string)

3、Split ,以子串分割,返回[]string

func strings.Split(s string, sep string) []string

4、ReplaceAll 替换

func strings.ReplaceAll(s, old, new string) string

5、其他

func Clone(s string) string
func Compare(a, b string) int
func Contains(s, substr string) bool
func ContainsAny(s, chars string) bool
func ContainsFunc(s string, f func(rune) bool) bool
func ContainsRune(s string, r rune) bool
func Count(s, substr string) int
func Cut(s, sep string) (before, after string, found bool)
func CutPrefix(s, prefix string) (after string, found bool)
func CutSuffix(s, suffix string) (before string, found bool)
func EqualFold(s, t string) bool
func Fields(s string) []string
func FieldsFunc(s string, f func(rune) bool) []string
func HasPrefix(s, prefix string) bool
func HasSuffix(s, suffix string) bool
func Index(s, substr string) int
func IndexAny(s, chars string) int
func IndexByte(s string, c byte) int
func IndexFunc(s string, f func(rune) bool) int
func IndexRune(s string, r rune) int
func Join(elems []string, sep string) string
func LastIndex(s, substr string) int
func LastIndexAny(s, chars string) int
func LastIndexByte(s string, c byte) int
func LastIndexFunc(s string, f func(rune) bool) int
func Map(mapping func(rune) rune, s string) string
func Repeat(s string, count int) string
func Replace(s, old, new string, n int) string
func ReplaceAll(s, old, new string) string
func Split(s, sep string) []string
func SplitAfter(s, sep string) []string
func SplitAfterN(s, sep string, n int) []string
func SplitN(s, sep string, n int) []string
func Title(s string) stringDEPRECATED
func ToLower(s string) string
func ToLowerSpecial(c unicode.SpecialCase, s string) string
func ToTitle(s string) string
func ToTitleSpecial(c unicode.SpecialCase, s string) string
func ToUpper(s string) string
func ToUpperSpecial(c unicode.SpecialCase, s string) string
func ToValidUTF8(s, replacement string) string
func Trim(s, cutset string) string
func TrimFunc(s string, f func(rune) bool) string
func TrimLeft(s, cutset string) string
func TrimLeftFunc(s string, f func(rune) bool) string
func TrimPrefix(s, prefix string) string
func TrimRight(s, cutset string) string
func TrimRightFunc(s string, f func(rune) bool) string
func TrimSpace(s string) string
func TrimSuffix(s, suffix string) string
type Builder
	func (b *Builder) Cap() int
	func (b *Builder) Grow(n int)
	func (b *Builder) Len() int
	func (b *Builder) Reset()
	func (b *Builder) String() string
	func (b *Builder) Write(p []byte) (int, error)
	func (b *Builder) WriteByte(c byte) error
	func (b *Builder) WriteRune(r rune) (int, error)
	func (b *Builder) WriteString(s string) (int, error)
type Reader
	func NewReader(s string) *Reader
	func (r *Reader) Len() int
	func (r *Reader) Read(b []byte) (n int, err error)
	func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)
	func (r *Reader) ReadByte() (byte, error)
	func (r *Reader) ReadRune() (ch rune, size int, err error)
	func (r *Reader) Reset(s string)
	func (r *Reader) Seek(offset int64, whence int) (int64, error)
	func (r *Reader) Size() int64
	func (r *Reader) UnreadByte() error
	func (r *Reader) UnreadRune() error
	func (r *Reader) WriteTo(w io.Writer) (n int64, err error)
type Replacer
	func NewReplacer(oldnew ...string) *Replacer
	func (r *Replacer) Replace(s string) string
	func (r *Replacer) WriteString(w io.Writer, s string) (n int, err error)

-------------end文章来源地址https://www.toymoban.com/news/detail-642849.html

到了这里,关于golang strings 常用函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包