golang 字串操作例項

2021-08-19 11:45:15 字數 635 閱讀 7130

package main

import s "strings"

import "fmt"

var p = fmt.println

func main() , "-"))//字串陣列 連線 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

}

golang之字串操作

1.字串在另乙個字串第一次出現的位置 str hello world fmt.println strings.index str,world 62.字串在另乙個字串最後一次出現的位置 fmt.println strings.lastindex str,l 93.切割字串 fmt.println st...

Golang 字串型別和字串操作

package main import fmt func main 標準輸出 bo y 我來自 t中國package main import fmt func main 標準輸出 我叫boy false true true true false false truepackage main impo...

golang對字串的操作

字串操作在程式設計中佔了很大的比重。package main import bytes fmt strconv strings unicode 簡單實用string操作符 func contains s,substr string bool func join a string,sep string...