紀錄一些常用,可是每次要用就會忘記的字串處理工具....
1.字串轉換
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
/*布林*/
b, _ := strconv.ParseBool("true") //string => bool
s := strconv.FormatBool(b) //bool => string
fmt.Println(s)
/*浮點數*/
f, _ := strconv.ParseFloat("3.1415", 64) //string => float64
s = strconv.FormatFloat(f, 'f', -1, 64) //float64 => string
fmt.Println(s)
//浮點數轉換格式表
//'b' (-ddddp±ddd, a binary exponent)
//'e' (-d.dddde±dd, a decimal exponent)
//'E' (-d.ddddE±dd, a decimal exponent)
//'f' (-ddd.dddd, no exponent)
//'g' ('e' for large exponents, 'f' otherwise)
//'G' ('E' for large exponents, 'f' otherwise)
//'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent)
//'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent)
/*整數*/
i, _ := strconv.ParseInt("-42", 10, 64) //string => int64 (10進制)
s = strconv.FormatInt(i, 10) //int64=> string (10進制)
fmt.Println(i)
/*正整數*/
u, _ := strconv.ParseUint("42", 10, 64) //string => uint64 (10進制)
s = strconv.FormatUint(u, 16) //uint64=> string (16進制)
fmt.Println(s)
/*時間*/
//"2006/01/02 15:04:05.000" 如同"yyyy/mm/dd hh:MM:ss.mmm"
now := time.Now().Format("2006/01/02 15:04:05.000") //時間轉格式化字串
t, _ := time.Parse("2006/01/02 15:04:05.000", now) //格式化字串轉時間
fmt.Println(t)
}
2.字串處理
package main
import (
"fmt"
"strings"
)
func main() {
/*替代*/
s := strings.ReplaceAll("123456", "345", "abc")
fmt.Println("Replace: ", s)
/*有沒有包含?*/
if strings.Contains("123456", "123") {
fmt.Println("yes")
}
/*切切*/
lines := strings.Split("Line1\nLine2\nLine3", "\n")
for _, line := range lines {
fmt.Println(line)
}
/*在哪裡*/
ss := "12345abc6"
s = "abc"
i := strings.Index(ss, s) //找不到會回-1
fmt.Println("index: ", i)
fmt.Println(ss[:i] + ss[i+len(s):]) //123456 去掉abc
/*重頭尾去掉不要的字元(可以指定)*/
fmt.Println(strings.Trim("要不要喝水? 我不要", "不要"))
/*重頭尾去掉不要的字元(會顯示空白的字元)*/
fmt.Println(strings.TrimSpace("\r\n \t123 4"))
/*大小很重要嗎*/
s = "aBcDeFg"
fmt.Println(strings.ToLower(s)) //abcdefg
fmt.Println(strings.ToUpper(s)) //ABCDEFG
/*格式化字串輸出*/
type Person struct {
Name string
Age int
}
p := Person{
Name: "Rex",
Age: 18,
}
s = fmt.Sprintf("%+v", p)
fmt.Println(s)
/*只列常用的*/
// Gerinal:
// %v the value in a default format,when printing structs,
// the plus flag (%+v) adds field names
// %#v a Go-syntax representation of the value
// %T a Go-syntax representation of the type of the value
// %% a literal percent sign; consumes no value
// Boolean:
// %t the word true or false
// Integer:
// %b base 2
// %c the character represented by the corresponding Unicode code
// %d base 10
// %x base 16, with lower-case letters for a-f
// %X base 16, with upper-case letters for A-F
// Floating-point
// %f decimal point but no exponent, e.g. 123.456
// %9f width 9, default precision
// %.2f default width, precision 2
// %9.2f width 9, precision 2
// %9.f width 9, precision 0
// String and slice of bytes (treated equivalently with these verbs):
// %s the uninterpreted bytes of the string or slice
// %q a double-quoted string safely escaped with Go syntax
// %x base 16, lower-case, two characters per byte
// %X base 16, upper-case, two characters per byte
}
參考來源:
1.https://golang.org/pkg/strconv/
2.https://golang.org/pkg/time/