zoukankan      html  css  js  c++  java
  • golang字符串常用操作

    获取字符串的长度

    package main
     
    
    import (
        "fmt"
    )
     
    
    var name string = "helloworld"
     
    
    func main() {
        fmt.Println(len(name ))
    }
     

    字符串拼接

    func main() {
        info := name + fmt.Sprintf(" %d", age)
        fmt.Println(info)
    }

     字符串切割

      
     
    var name string = "helloworld"
    
    func main() {
      info := strings.Split(name, "ll")
    
    
        fmt.Println(info)

    }

    判断是否包含

    var name string = "helloworld"
    
    func main() {
        if strings.Contains(name, "h") {
            fmt.Println("Contains")
        }
    }

    判断前缀后缀

    var name string = "helloworld"
    
    func main() {
        fmt.Println(strings.HasPrefix(name, "h"))
        fmt.Println(strings.HasSuffix(name, "d"))
    }

    判断字符出现的索引位置

    var name string = "helloworld"
    
    func main() {
        fmt.Println(strings.Index(name, "l"))
        fmt.Println(strings.LastIndex(name, "l"))
    }

    切片元素拼接字符串

    var name string = "helloworld"
    
    func main() {
        nameList := make([]string, 0)
        nameList = append(nameList, name)
        nameList = append(nameList, name)
        fmt.Println(strings.Join(nameList, ","))
    }

    字符串转义符

    Go 语言的字符串常见转义符包含回车、换行、单双引号、制表符等,如下表所示。

    
              回车符
    
    
              换行
    
    	          制表符
    
    \          反斜杠
  • 相关阅读:
    react-document-title
    react-router
    redux-saga 异步流
    redux
    redux-thunk
    react-router-redux
    [翻译] ClockView 时钟
    [翻译] MZTimerLabel 用作秒表或者倒计时
    [翻译] MCProgressView 使用自定义图片做进度显示
    [翻译] ADPopupView 触摸弹出视窗
  • 原文地址:https://www.cnblogs.com/Nolover/p/12510838.html
Copyright © 2011-2022 走看看