• go 字符串方法


    package main

    import (
    "fmt"
    "strconv"
    "strings"
    )
    func main() {
    //1.strings.HasPrefix(s string, prefix string) bool:判断字符串s是否以prefix开头
    fmt.Println(strings.HasPrefix("hello world", "he")) //true
    //2.strings.HasSuffix(s string, suffix string) bool:判断字符串s是否以suffix结尾
    fmt.Println(strings.HasSuffix("hello world", "ll")) //false
    //3.strings.Index(s string, str string) int:判断str在s中首次出现的位置,如果没有出现,则返回-1
    fmt.Println(strings.Index("hello world", "he")) //0
    //4.strings.LastIndex(s string, str string) int:判断str在s中最后出现的位置,如果没有出现,则返回-1
    fmt.Println(strings.LastIndex("hello world", "or")) //7
    //5.strings.Replace(str string, old string, new string, n int):字符串替换
    fmt.Println(strings.Replace("he12he wohe12", "he", "ww", 2)) //ww12ww wohe12
    //6.strings.Count(str string, substr string)int:字符串计数
    fmt.Println(strings.Count("hehehe123he123eheh", "he")) //5
    //7.strings.Repeat(str string, count int)string:重复count次str
    fmt.Println(strings.Repeat("hello", 5)) //hellohellohellohellohello
    //8.strings.ToLower(str string)string:转为小写
    fmt.Println(strings.ToLower("HELLO")) //hello
    //9.strings.ToUpper(str string)string:转为大写
    fmt.Println(strings.ToUpper("hello")) //HELLO
    //10.strings.TrimSpace(str string):去掉字符串首尾空白字符
    fmt.Println(strings.TrimSpace(" hello world ")) //hello world
    //11.strings.Trim(str string, cut string):去掉字符串首尾cut字符
    fmt.Println(strings.Trim("abchelloba", "ab")) //chello
    //12.strings.TrimLeft(str string, cut string):去掉字符串首cut字符
    fmt.Println(strings.TrimLeft("abchelloba", "ab")) //chelloba
    //13.strings.TrimRight(str string, cut string):去掉字符串首cut字符
    fmt.Println(strings.TrimRight("abchelloba", "ab")) //abchello
    //14.strings.Fields(str string):返回str空格分隔的所有子串的slice
    fmt.Println(strings.Fields("hello world abc")) //[hello world abc]
    //15.strings.Split(str string, split string):返回str split分隔的所有子串的slice
    fmt.Println(strings.Split("abca12a34a", "a")) //[ bc 12 34 ]
    //16.strings.Join(s1 []string, sep string):用sep把s1中的所有元素链接起来
    fmt.Println(strings.Join([]string{"123", "abc", "456"}, "xx")) //123xxabcxx456
    //17.strconv.Itoa(i int):把一个整数i转成字符串
    fmt.Println(strconv.Itoa(3)) //"3"
    //18.strconv.Atoi(str string)(int, error):把一个字符串转成整数
    fmt.Println(strconv.Atoi("38")) //38 nil
  • 相关阅读:
    css 模块化
    zookeeper集群-solrcloud集群
    Ansible
    Haproxy
    Saltstack数据系统
    Saltstack入门
    DNS(bind)添加A、CNAME、MX、PTR记录、智能DNS(ACL)
    主从同步DNS(BIND)
    单台DNS服务器搭建(BIND)
    python2.7多线程的批量操作远程服务器
  • 原文地址:https://www.cnblogs.com/yangxinpython/p/12982196.html
Copyright © 2020-2023  润新知