golang 字符串操作实例

package main
 
import s "strings"
import "fmt"
 
var p = fmt.Println
 
func main() {
    p("Contains: ", s.Contains("test", "es")) //是否包含 true
    p("Count: ", s.Count("test", "t")) //字符串出现字符的次数 2
    p("HasPrefix: ", s.HasPrefix("test", "te")) //判断字符串首部 true
    p("HasSuffix: ", s.HasSuffix("test", "st")) //判断字符串结尾 true
    p("Index: ", s.Index("test", "e")) //查询字符串位置 1
    p("Join: ", s.Join([]string{"a", "b"}, "-"))//字符串数组 连接 a-b
    p("Repeat: ", s.Repeat("a", 5)) //重复一个字符串 aaaaa
    p("Replace: ", s.Replace("foo", "o", "0", -1)) //字符串替换 指定起始位置为小于0,则全部替换 f00
    p("Replace: ", s.Replace("foo", "o", "0", 1)) //字符串替换 指定起始位置1 f0o
    p("Split: ", s.Split("a-b-c-d-e", "-")) //字符串切割 [a b c d e]
    p("ToLower: ", s.ToLower("TEST")) //字符串 小写转换 test
    p("ToUpper: ", s.ToUpper("test")) //字符串 大写转换 TEST
    p("Len: ", len("hello")) //字符串长度
    p("Char:", "hello"[1]) //标取字符串中的字符,类型为byte
}

  

相关文章

类型转换 1、int转string 2、string转int 3、string转float ...
package main import s "strings" import...
类使用:实现一个people中有一个sayhi的方法调用功能,代码如...
html代码: beego代码:
1、读取文件信息: 2、读取文件夹下的所有文件: 3、写入文件...
配置环境:Windows7+推荐IDE:LiteIDEGO下载地址:http:...