strings.HasPrefix(str, "AB") strings.HasSuffix(str, "AB") 判断是否AB开头,是否AB结尾
strings.Contains(str, "A")是否包含子字符串
strings.Index(str, "d") 判断位置默认0,找不到-1,LastIndex是最后出现位置
strings.IndexRune(str, rune('A')) 用于转换单字符串,可以是汉字,必须单引号
strings.Replace(str, old, new, n)字符串替换,n是-1替换所有
strings.Count(s, str string) 统计子字符串出现重复次数
strings.Repeat(str, 2) 重复输出字符串2次
ToLower ToUpper 转换小写 大写
TrimSpace去除开头结尾空格
Trim(s, "cut") 剔除两边指定字符串 TrimLeft剔除左边字符串,默认是空白可指TrimRight右边字符串剔除
str=strings.ReplaceAll(str,” “,””) 去掉全部空格
strings.Split(str, "-")字符串用-分割,返回slice explode
str :=[]string{"abc","def","hij"} strings.Join(str,"-")切片字符串用-连接成字符串 implode

s := strconv.Itoa(-42) 数字int转换字符串
i, err := strconv.Atoi("-42") 字符串转换int
i64, err := strconv.ParseInt(s, 10, 64) 字符串转换int64
b, err := strconv.ParseBool(“true”)
f, err := strconv.ParseFloat(“3.1415”, 64) 转换成浮点
u, err := strconv.ParseUint(“42”, 10, 64)
s := strconv.FormatBool(true)
s := strconv.FormatFloat(3.1415, ‘E’, -1, 64)
s := strconv.FormatInt(-42, 16)
s := strconv.FormatUint(42, 16)

q := strconv.Quote(“Hello, 世界”) 转换成带引号字符串
q1 := strconv.QuoteToASCII(“Hello, 世界”) \u转换中文

map转换json字符串相互转换

t1 := make(map[string]interface{})
    t1["company"] = "itcast"
    t1["subjects"] = []string{"Go", "C++", "Python", "测试"}
    t1["isok"] = true
    t1["price"] = 666.666
    b, _ := json.Marshal(t1)
    b1:=string(b)
    fmt.Println(b1)
    var b2 map[string]interface{}
    json.Unmarshal([]byte(b1),&b2)
    fmt.Println(b2)

//排序


    m:=map[string]interface{}{
        "1":"ok",
        "4":12,
        "3":"123",
        "2":"我爱你",
    }
    fmt.Println(m)
    key := make([]int, 0)
    for k,_:=range m{
        k,_:=strconv.Atoi(k)
        key= append(key,k)
    }
    //sort.Ints(key)//升序
    //sort.Float64s
    // sort.Strings
    sort.Sort(sort.Reverse(sort.IntSlice(key)))//降序
    //sort.Sort(sort.Reverse(sort.Float64Slice(f)))//浮点
    //sort.Sort(sort.Reverse(sort.StringSlice(s)))//字符
    for _, v := range key  {
        v:=strconv.Itoa(v)
        fmt.Println(m[v])
    }
作者:Yoby  创建时间:2020-09-11 23:05
 更新时间:2024-12-05 13:26
上一篇:
下一篇: