字符串函数在golang中的使用

PowerMichael · · 899 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。


package main

import (
    "fmt"
    "strings"
)

func main()  {
    //判断父字符串是否包含子字符串,包含为真,否则为假
    fmt.Println("-------------------------------")
    fmt.Println("contains Function:", strings.Contains("Austrilia", "lia"))
    fmt.Println("contains Function:", strings.Contains("Austrilia", "mia"))

    //子字符串在父字符串中出现的次数
    fmt.Println("-------------------------------")
    fmt.Println("Count Function: ",strings.Count("Australia", "lia"))
    fmt.Println("Count Function: ",strings.Count("Australia", "A"))
    fmt.Println("Count Function: ",strings.Count("Australia", "Bu"))

    //判断子字符串是否为父字符串的前缀
    fmt.Println("-------------------------------")
    fmt.Println("HasPrefix Function: ",strings.HasPrefix("Australia", "Bu"))
    fmt.Println("HasPrefix Function: ",strings.HasPrefix("Australia", "Aus"))

    ////判断子字符串是否为父字符串的后缀
    fmt.Println("-------------------------------")
    fmt.Println("HasSuffix Function: ",strings.HasSuffix("Australia", "lia"))
    fmt.Println("HasSuffix Function: ",strings.HasSuffix("Australia", "tia"))

    //子字符串首次出现在父子符串的索引下标
    fmt.Println("-------------------------------")
    fmt.Println("Index Function:",strings.Index("Australia", "lia"))
    fmt.Println("Index Function: ",strings.Index("Australia", "a"))
    fmt.Println("Index Function: ",strings.Index("Australia", "A"))

    //连接函数,指定分隔符将字符串连接起来
    fmt.Println("-------------------------------")
    fmt.Println("Join Functions:",strings.Join([]string{"Austrilia", "Japen", "Germany"}, "-"))
    fmt.Println("Join Function: ",strings.Join([]string{"Australia","Japan","Germany"}," "))

    //重复函数,指定字符串重复出现的次数
    fmt.Println("-------------------------------")
    fmt.Println("Repeat Function:", strings.Repeat("Australia",3))

    //替换字符串函数,
    fmt.Println("-------------------------------")
    fmt.Println("Replace Function: ",strings.Replace("Australia ","a","b",1))
    fmt.Println("Replace Function: ",strings.Replace("Australia ","a","b",2))
    fmt.Println("Replace Function: ",strings.Replace("Aaustralia ","a","b",3))
    fmt.Println("Replace Function: ",strings.Replace("Australia ","A","B",1))
    fmt.Println("Replace Function: ",strings.Replace("Australia ","lia","pia",1))

    //分割函数,指定分隔符分隔字符串
    fmt.Println("-------------------------------")
    fmt.Println("Split Function:", strings.Split("Australia-Japan-Germany","-"))

    //把字符转小写
    fmt.Println("-------------------------------")
    fmt.Println("ToLower Function: ",strings.ToLower("Australia"))

    //把字符串转大写
    fmt.Println("-------------------------------")
    fmt.Println("ToUpper Function: ",strings.ToUpper("Australia"))

}

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

本文来自:51CTO博客

感谢作者:PowerMichael

查看原文:字符串函数在golang中的使用

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

899 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传