Go语言的strings包字符串处理二之字符串操作函数

栏目: Go · 发布时间: 6年前

Contains

func   Contains( s ,   substr   string )   bool

功能:字符串 s 中是否包含 substr ,返回 bool

示例代码:

fmt . Println ( strings . Contains ( "seafood" ,   "foo" ))

fmt . Println ( strings . Contains ( "seafood" ,   "bar" ))

fmt . Println ( strings . Contains ( "seafood" ,   "" ))

fmt . Println ( strings . Contains ( "" ,   "" ))

// 运行结果 :

//true

//false

//true

//true

Join

func   Join( a   [] string ,   sep   string )   string

功能:字符串链接,把 slice   a 通过 sep 链接起来

示例代码:

s   :=   [] string { "foo" ,   "bar" ,   "baz" }

fmt . Println ( strings . Join ( s ,   ",   " ))

// 运行结果 :foo,   bar,   baz

Index

func   Index( s ,   sep   string )   int

功能:在字符串 s 中查找 sep 所在的位置,返回位置值,找不到返回 - 1

示例代码:

fmt . Println ( strings . Index ( "chicken" ,   "ken" ))

fmt . Println ( strings . Index ( "chicken" ,   "dmr" ))

// 运行结果 :

//      4

//      -1

Repeat

func   Repeat( s   string ,   count   int )   string

功能:重复 s 字符串 count 次,最后返回重复的字符串

示例代码:

fmt . Println ( "ba"   +   strings . Repeat ( "na" ,   2 ))

// 运行结果 :banana

Replace

func   Replace( s ,   old ,   new   string ,   n   int )   string

功能:在 s 字符串中,把 old 字符串替换为 new 字符串, n 表示替换的次数,小于 0 表示全部替换

示例代码:

fmt . Println ( strings . Replace ( "oink   oink   oink" ,   "k" ,   "ky" ,   2 ))

fmt . Println ( strings . Replace ( "oink   oink   oink" ,   "oink" ,   "moo" ,   - 1 ))

// 运行结果 :

//oinky   oinky   oink

//moo   moo   moo

Split

func   Split( s ,   sep   string )   [] string

功能:把 s 字符串按照 sep 分割,返回 slice

示例代码:

fmt . Printf ( "%q\n" ,   strings . Split ( "a,b,c" ,   "," ))

fmt . Printf ( "%q\n" ,   strings . Split ( "a   man   a   plan   a   canal   panama" ,   "a   " ))

fmt . Printf ( "%q\n" ,   strings . Split ( "   xyz   " ,   "" ))

fmt . Printf ( "%q\n" ,   strings . Split ( "" ,   "Bernardo   O'Higgins" ))

// 运行结果 :

//["a"   "b"   "c"]

//[""   "man   "   "plan   "   "canal   panama"]

//["   "   "x"   "y"   "z"   "   "]

//[""]

Trim

func   Trim( s   string ,   cutset   string )   string

功能:在 s 字符串的头部和尾部去除 cutset 指定的字符串

示例代码:

fmt . Printf ( "[%q]" ,   strings . Trim ( "   !!!   Achtung   !!!   " ,   "!   " ))

// 运行结果 :["Achtung"]

Fields

func   Fields( s   string )   [] string

功能:去除 s 字符串的空格符,并且按照空格分割返回 slice

示例代码:

fmt . Printf ( "Fields   are:   %q" ,   strings . Fields ( "    foo   bar    baz     " ))

// 运行结果 :Fields   are:   ["foo"   "bar"   "baz"]


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Art of Computer Programming, Volume 3

The Art of Computer Programming, Volume 3

Donald E. Knuth / Addison-Wesley Professional / 1998-05-04 / USD 74.99

Finally, after a wait of more than thirty-five years, the first part of Volume 4 is at last ready for publication. Check out the boxed set that brings together Volumes 1 - 4A in one elegant case, and ......一起来看看 《The Art of Computer Programming, Volume 3》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码