Files
build-web-application-with-…/zh-tw/07.6.md
2019-02-26 01:40:54 +08:00

182 lines
4.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 7.6 字串處理
字串在我們平常的Web開發中經常用到包括使用者的輸入資料庫讀取的資料等我們經常需要對字串進行分割、連線、轉換等操作本小節將透過Go標準函式庫中的strings和strconv兩個套件中的函式來講解如何進行有效快速的操作。
## 字串操作
下面這些函式來自於strings套件這裡介紹一些我平常經常用到的函式更詳細的請參考官方的文件。
- func Contains(s, substr string) bool
字串s中是否包含substr返回bool值
```Go
fmt.Println(strings.Contains("seafood", "foo"))
fmt.Println(strings.Contains("seafood", "bar"))
fmt.Println(strings.Contains("seafood", ""))
fmt.Println(strings.Contains("", ""))
//Output:
//true
//false
//true
//true
```
- func Join(a []string, sep string) string
字串連結把slice a透過sep連結起來
```Go
s := []string{"foo", "bar", "baz"}
fmt.Println(strings.Join(s, ", "))
//Output:foo, bar, baz
```
- func Index(s, sep string) int
在字串s中查詢sep所在的位置返回位置值找不到返回-1
```Go
fmt.Println(strings.Index("chicken", "ken"))
fmt.Println(strings.Index("chicken", "dmr"))
//Output:4
//-1
```
- func Repeat(s string, count int) string
重複s字串count次最後返回重複的字串
```Go
fmt.Println("ba" + strings.Repeat("na", 2))
//Output:banana
```
- func Replace(s, old, new string, n int) string
在s字串中把old字串替換為new字串n表示替換的次數小於0表示全部替換
```Go
fmt.Println(strings.Replace("oink oink oink", "k", "ky", 2))
fmt.Println(strings.Replace("oink oink oink", "oink", "moo", -1))
//Output:oinky oinky oink
//moo moo moo
```
- func Split(s, sep string) []string
把s字串按照sep分割返回slice
```Go
fmt.Printf("%q\n", strings.Split("a,b,c", ","))
fmt.Printf("%q\n", strings.Split("a man a plan a canal panama", "a "))
fmt.Printf("%q\n", strings.Split(" xyz ", ""))
fmt.Printf("%q\n", strings.Split("", "Bernardo O'Higgins"))
//Output:["a" "b" "c"]
//["" "man " "plan " "canal panama"]
//[" " "x" "y" "z" " "]
//[""]
```
- func Trim(s string, cutset string) string
在s字串的頭部和尾部去除cutset指定的字串
```Go
fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
//Output:["Achtung"]
```
- func Fields(s string) []string
去除s字串的空格符並且按照空格分割返回slice
```Go
fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
//Output:Fields are: ["foo" "bar" "baz"]
```
## 字串轉換
字串轉化的函式在strconv中如下也只是列出一些常用的
- Append 系列函式將整數等轉換為字串後,新增到現有的位元組陣列中。
```Go
package main
import (
"fmt"
"strconv"
)
func main() {
str := make([]byte, 0, 100)
str = strconv.AppendInt(str, 4567, 10)
str = strconv.AppendBool(str, false)
str = strconv.AppendQuote(str, "abcdefg")
str = strconv.AppendQuoteRune(str, '單')
fmt.Println(string(str))
}
```
- Format 系列函式把其他型別的轉換為字串
```Go
package main
import (
"fmt"
"strconv"
)
func main() {
a := strconv.FormatBool(false)
b := strconv.FormatFloat(123.23, 'g', 12, 64)
c := strconv.FormatInt(1234, 10)
d := strconv.FormatUint(12345, 10)
e := strconv.Itoa(1023)
fmt.Println(a, b, c, d, e)
}
```
- Parse 系列函式把字串轉換為其他型別
```Go
package main
import (
"fmt"
"strconv"
)
func checkError(e error){
if e != nil{
fmt.Println(e)
}
}
func main() {
a, err := strconv.ParseBool("false")
checkError(err)
b, err := strconv.ParseFloat("123.23", 64)
checkError(err)
c, err := strconv.ParseInt("1234", 10, 64)
checkError(err)
d, err := strconv.ParseUint("12345", 10, 64)
checkError(err)
e, err := strconv.Atoi("1023")
checkError(err)
fmt.Println(a, b, c, d, e)
}
```
## links
* [目錄](<preface.md>)
* 上一節: [檔案操作](<07.5.md>)
* 下一節: [小結](<07.7.md>)