Merging other languages
This commit is contained in:
314
en/07.6.md
314
en/07.6.md
@@ -1,152 +1,162 @@
|
||||
# 7.6 字符串处理
|
||||
字符串在我们平常的Web开发中经常用到,包括用户的输入,数据库读取的数据等,我们经常需要对字符串进行分割、连接、转换等操作,本小节将通过Go标准库中的strings和strconv两个包中的函数来讲解如何进行有效快速的操作。
|
||||
## 字符串操作
|
||||
下面这些函数来自于strings包,这里介绍一些我平常经常用到的函数,更详细的请参考官方的文档。
|
||||
|
||||
- func Contains(s, substr string) bool
|
||||
|
||||
字符串s中是否包含substr,返回bool值
|
||||
|
||||
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链接起来
|
||||
|
||||
s := []string{"foo", "bar", "baz"}
|
||||
fmt.Println(strings.Join(s, ", "))
|
||||
//Output:foo, bar, baz
|
||||
|
||||
- func Index(s, sep string) int
|
||||
|
||||
在字符串s中查找sep所在的位置,返回位置值,找不到返回-1
|
||||
|
||||
fmt.Println(strings.Index("chicken", "ken"))
|
||||
fmt.Println(strings.Index("chicken", "dmr"))
|
||||
//Output:4
|
||||
//-1
|
||||
|
||||
- func Repeat(s string, count int) string
|
||||
|
||||
重复s字符串count次,最后返回重复的字符串
|
||||
|
||||
fmt.Println("ba" + strings.Repeat("na", 2))
|
||||
//Output:banana
|
||||
|
||||
- func Replace(s, old, new string, n int) string
|
||||
|
||||
在s字符串中,把old字符串替换为new字符串,n表示替换的次数,小于0表示全部替换
|
||||
|
||||
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
|
||||
|
||||
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指定的字符串
|
||||
|
||||
fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
|
||||
//Output:["Achtung"]
|
||||
|
||||
- func Fields(s string) []string
|
||||
|
||||
去除s字符串的空格符,并且按照空格分割返回slice
|
||||
|
||||
fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
|
||||
//Output:Fields are: ["foo" "bar" "baz"]
|
||||
|
||||
|
||||
## 字符串转换
|
||||
字符串转化的函数在strconv中,如下也只是列出一些常用的:
|
||||
|
||||
- Append 系列函数将整数等转换为字符串后,添加到现有的字节数组中。
|
||||
|
||||
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 系列函数把其他类型的转换为字符串
|
||||
|
||||
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 系列函数把字符串转换为其他类型
|
||||
|
||||
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>)
|
||||
# 7.6 Strings
|
||||
|
||||
On the web, almost everything we see (including user inputs, database access, etc.), is represented by strings. They are a very important part of web development. In many cases, we also need to split, join, convert and otherwise manipulate strings. In this section, we are going to introduce the `strings` and `strconv` packages from the Go standard library.
|
||||
|
||||
## strings
|
||||
|
||||
The following functions are from the `strings` package. See the official documentation for more details:
|
||||
|
||||
- func Contains(s, substr string) bool
|
||||
|
||||
Check if string `s` contains string `substr`, returns a boolean value.
|
||||
|
||||
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
|
||||
|
||||
Combine strings from slice with separator `sep`.
|
||||
|
||||
s := []string{"foo", "bar", "baz"}
|
||||
fmt.Println(strings.Join(s, ", "))
|
||||
//Output:foo, bar, baz
|
||||
|
||||
- func Index(s, sep string) int
|
||||
|
||||
Find index of `sep` in string `s`, returns -1 if it's not found.
|
||||
|
||||
fmt.Println(strings.Index("chicken", "ken"))
|
||||
fmt.Println(strings.Index("chicken", "dmr"))
|
||||
//Output:4
|
||||
//-1
|
||||
|
||||
- func Repeat(s string, count int) string
|
||||
|
||||
Repeat string `s` `count` times.
|
||||
|
||||
fmt.Println("ba" + strings.Repeat("na", 2))
|
||||
//Output:banana
|
||||
|
||||
- func Replace(s, old, new string, n int) string
|
||||
|
||||
Replace string `old` with string `new` in string `s`. `n` is the number of replacements. If n is less than 0, replace all instances.
|
||||
|
||||
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
|
||||
|
||||
Split string `s` with separator `sep` into a slice.
|
||||
|
||||
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
|
||||
|
||||
Remove `cutset` of string `s` if it's leftmost or rightmost.
|
||||
|
||||
fmt.Printf("[%q]", strings.Trim(" !!! Achtung !!! ", "! "))
|
||||
Output:["Achtung"]
|
||||
|
||||
- func Fields(s string) []string
|
||||
|
||||
Remove space items and split string with space into a slice.
|
||||
|
||||
fmt.Printf("Fields are: %q", strings.Fields(" foo bar baz "))
|
||||
//Output:Fields are: ["foo" "bar" "baz"]
|
||||
|
||||
|
||||
## strconv
|
||||
|
||||
The following functions are from the `strconv` package. As usual, please see official documentation for more details:
|
||||
|
||||
- Append series, convert data to string, and append to current byte slice.
|
||||
|
||||
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 series, convert other data types into string.
|
||||
|
||||
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 series, convert strings to other types.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
a, err := strconv.ParseBool("false")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
b, err := strconv.ParseFloat("123.23", 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
c, err := strconv.ParseInt("1234", 10, 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
d, err := strconv.ParseUint("12345", 10, 64)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
e, err := strconv.Itoa("1023")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(a, b, c, d, e)
|
||||
}
|
||||
|
||||
## Links
|
||||
|
||||
- [Directory](preface.md)
|
||||
- Previous section: [Files](07.5.md)
|
||||
- Next section: [Summary](07.7.md)
|
||||
|
||||
Reference in New Issue
Block a user