提交 0910e527 编写于 作者: V vCaesar

Add 0.7.6.md syntax highlighting

上级 89f1b3f8
......@@ -6,7 +6,9 @@
- 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", ""))
......@@ -17,43 +19,56 @@
//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 ", ""))
......@@ -62,27 +77,35 @@
//["" "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 (
......@@ -98,8 +121,10 @@
str = strconv.AppendQuoteRune(str, '单')
fmt.Println(string(str))
}
```
- Format 系列函数把其他类型的转换为字符串
```Go
package main
......@@ -117,8 +142,12 @@
fmt.Println(a, b, c, d, e)
}
```
- Parse 系列函数把字符串转换为其他类型
```Go
package main
import (
......@@ -144,7 +173,7 @@
fmt.Println(a, b, c, d, e)
}
```
## links
* [目录](<preface.md>)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册