教程集 www.jiaochengji.com
教程集 >  Golang编程  >  golang教程  >  正文 golang之字符串操作

golang之字符串操作

发布时间:2022-03-26   编辑:jiaochengji.com
教程集为您提供golang之字符串操作等资源,欢迎您收藏本站,我们将为您提供最新的golang之字符串操作资源

1.字符串在另一个字符串第一次出现的位置

<pre class="has"><code>str := "hello world" fmt.Println(strings.Index(str, "world")) //6</code></pre>

2.字符串在另一个字符串最后一次出现的位置

<pre class="has"><code>fmt.Println(strings.LastIndex(str, "l")) //9</code></pre>

3.切割字符串

<pre class="has"><code>fmt.Println(strings.Split(str, "l")) // [he o wor d]</code></pre>

4.字符串数组转化为字符串

<pre class="has"><code>list := []string{"he", "o", "word"}; fmt.Println(strings.Join(list, "-")) // he-o-word</code></pre>

5.字符串是否已另一个字符串开始或者结束

<pre class="has"><code>fmt.Println(strings.HasPrefix(str, "hello")) //true fmt.Println(strings.HasSuffix(str, "hello")) //false</code></pre>

6.字符串大小写转化

<pre class="has"><code>fmt.Println(strings.ToUpper(str)) //HELLO WORLD fmt.Println(strings.ToLower(str)) //hello world</code></pre>

7.删除收尾特定字符串

<pre class="has"><code>fmt.Println(strings.Trim(str, "h")) //ello world</code></pre>

8.删除收尾空格

<pre class="has"><code>fmt.Println(strings.TrimSpace(str)) //hello world</code></pre>

9.字符串替换

<pre class="has"><code>fmt.Println(strings.Replace(str, "hello", "your", -1)) //your world</code></pre>

将str的字符串里的hello替换为your, 第四个参数为替换次数,-1为全部替换

到此这篇关于“golang之字符串操作”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!

您可能感兴趣的文章:
golang之字符串操作
go-redis使用之String字符串
Golang 字符串操作:字符串反转
Golang几种连接字符串方法
Golang 标准库:strconv 包的应用
Golang结构体中Tag的使用
PHP中字符串处理的一些常用函数
PHP中常用的18个字符串函数
golang时间转换
Golang将IP转为整型int存储

[关闭]
~ ~