4

【golang】字符串操作

 3 years ago
source link: https://studygolang.com/articles/32476
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【golang】字符串操作

七八个星天 · 1天之前 · 70 次点击 · 预计阅读时间 1 分钟 · 不到1分钟之前 开始浏览    

操作 表达式 返回值 判断字符串中是否包含某个子串 Contains("hello","he") true;false 将字符串切片中的元素组合成一个字符串 strings.Join([]string{"hello","world","!"}," ") hello world ! 查找子串在字符串中的位置 strings.Index("hello","ll ") 下标值;-1(子串不存在) 以指定字符拆分字符串 strings.Split("hello,world,everyone",",") [hello world everyone] 剪裁字符串两头的指定字符 strings.Trim("!!Hello world!!","!") Hello world 以空格区分,将字符串转换成字符串切片 strings.Fields(" Hello world ") [Hello world]
package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println(strings.Contains("hello","he"))
    fmt.Println(strings.Join([]string{"hello","world","!"}," "))
    fmt.Println(strings.Index("hello","l1l"))
    fmt.Println(strings.Split("hello,world,everyone",","))
    fmt.Println(strings.Trim("!!Hello world!!","!"))
    fmt.Println(strings.Fields("  Hello world   "))
}

运行结果:

true
hello world !
-1
[hello world everyone]
Hello world
[Hello world]

有疑问加站长微信联系(非本文作者)

280

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK