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
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM