go字符串数组以及字符串处理集合

package main

import (
    "fmt"
    "strconv"
    "path"
    "strings")
func main(){
   k :="1,2,3,"
   k = strings.TrimSuffix(k,",")  //修剪后缀  "1,2,3"
   k = strings.TrimPrefix(k,"1,")  // 修剪前缀 "2,3“
   strArr := strings.Split(k, `,`)  //分隔成数组 [2 3]
   fmt.Println(k,strArr,strings.Index(k,",")) //strings.Index 查找字符串首次出现的位置,不包含返回-1,包含返回首次出现的位置
   m :="test_user"
   id :="4"
   sql :=fmt.Sprintf("DELETE from %s WHERE id IN (%s)",m,k)
   fmt.Println(strings.Index(k,","),strings.Index(id,","),sql)
   //DB.Exec(sql) //mysql执行原生sql
   num,_ :=strconv.Atoi(id)//字符串转整
   numStr := strconv.Itoa(num)//整数转字符串
   fmt.Println(num,numStr)
   k = "1,2,3,4,5,6,7,8,9,10,11,12"
   kNew := strings.Replace(k,",","|",5) //替换指定次数字符串 1|2|3|4|5|6,7,8,9,10,11,12
   kNew = strings.ReplaceAll(k,",","|") //替换全部字符串 1|2|3|4|5|6|7|8|9|10|11|12
   kNew =kNew+" this is test !"
   fmt.Println(strings.TrimSpace(kNew)) 

     scopes := []string{"name1","name2","name3"}
    if in("name1",scopes) == true{
        fmt.Println("存在")
    }else{
        fmt.Println("不存在")
    }
    fmt.Println(getFileName("https://tx2.a.yximgs.com/uhead/AB/2018/12/25/15/BMjAxODEyMjUxNTA1MzBfMTAwMjIwMDQzN18xX2hkMjE0XzczMA==_s.jpg"))
     //BMjAxODEyMjUxNTA1MzBfMTAwMjIwMDQzN18xX2hkMjE0XzczMA==
}
/*遍历判断字符串在数组中存在*/
func in(target string, str_array []string) bool {
    for _, element := range str_array{
        if target == element{
            return true
        }
    }
    return false
}
/*提取url路径中想要的文件名*/
func getFileName(fullFilename string) string  {
    var filename string
    filename = path.Base(fullFilename)/*文件名 xxx.html*/
    filenameLen :=strings.Index(filename, "==") /*==出现在开始位置*/
    filename =filename[:filenameLen+2]/*提取包含==之前的内容*/
    return  filename
}

你可能感兴趣的:(go,golang)