zoukankan      html  css  js  c++  java
  • [GO]字符串的使用

    package main
    
    import (
        "fmt"
        "strings"
    )
    
    func main() {
        //判断字符串1是否包含字符串2,如果包含则返回true , 不包含则返回false
        fmt.Println(strings.Contains("hello go", "hello"))
        fmt.Println(strings.Contains("hello go", "abc"))
    
        //joins的组合
        s:=[]string{"abc", "hello", "mike", "go"}
        buf := strings.Join(s, "@")
        fmt.Println("buf = ", buf)
    
        //index, 查找子串的位置
        fmt.Println(strings.Index("abcdhello", "hello"))
        fmt.Println(strings.Index("abcd", "e")) //如果不包含子串的话则返回-1
    
        buf = strings.Repeat("go", 3) //重复三次打印
        fmt.Println("buf = ", buf)
    
        buf = "hello@go@mike"
        s2 := strings.Split(buf, "@") //打印以"@"分隔的结果
        fmt.Println("buf = ", s2)
    
        buf = strings.Trim("    are you ok    ", " ") //去掉字符串两边的空格
        fmt.Printf("buf = #%s#
    ", buf)
    
        buf1 := strings.Fields("     are you ok          ") //去掉所有的空格后返回一个切片
        for i, data := range buf1{
            fmt.Println(i, ", ", data)
        }
    }

    执行的结果为

    false
    buf =  abc@hello@mike@go
    4
    -1
    buf =  gogogo
    buf =  [hello go mike]
    buf = #are you ok#
    0 ,  are
    1 ,  you
    2 ,  ok
  • 相关阅读:
    react-redux: counter
    react table dropdown
    react pagination
    react privateRoute
    分布式爬虫(2)
    CentOS安装Python3.x
    spark复习笔记(3):使用spark实现单词统计
    《快学scala》读书笔记(2)
    《快学scala》读书笔记(1)
    spark复习笔记(2)
  • 原文地址:https://www.cnblogs.com/baylorqu/p/9661824.html
Copyright © 2011-2022 走看看