應用Barrier來掌握線程同步示例。本站提示廣大學習愛好者:(應用Barrier來掌握線程同步示例)文章只能為提供參考,不一定能成為您想要的結果。以下是應用Barrier來掌握線程同步示例正文
1.func Fields(s string) []string,這個函數的感化是依照1:n個空格來朋分字符串最初前往的是
[]string的切片
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Fields("hello widuu golang")) //out [hello widuu golang]
}
2.func FieldsFunc(s string, f func(rune) bool) []string一看就懂得了,這就是依據自界說函數朋分了
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.FieldsFunc("widuunhellonword", split)) // [widuu hello word]依據n字符朋分
}
func split(s rune) bool {
if s == 'n' {
return true
}
return false
}
3.func Join(a []string, sep string) string,這個跟php中的implode差不多,這個函數是將一個[]string的切片經由過程分隔符,朋分成一個字符串
import (
"fmt"
"strings"
)
func main() {
s := []string{"hello", "word", "xiaowei"}
fmt.Println(strings.Join(s, "-")) // hello-word-xiaowei
}
4.func Split(s, sep string) []string,有join就有Split這個就是把字符串依照指定的分隔符切割成slice
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.Split("a,b,c,d,e", ",")) //[a b c d e]
}
5.func SplitAfter(s, sep string) []string,這個函數是在前邊的切割完成以後再後邊在加上sep朋分符
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.SplitAfter("a,b,c,d", ",")) //[a, b, c, d]
}
6.func SplitAfterN(s, sep string, n int) []string該函數s依據sep朋分,前往朋分以後子字符串的slice,和split一樣,只是前往的子字符串保存sep,假如sep為空,那末每個字符都朋分
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.SplitAfterN("a,b,c,d,r", ",", 4)) //["a," "b," "c," "d,r"]
fmt.Println(strings.SplitAfterN("a,b,c,d,r", ",", 5)) //["a," "b," "c," "d," "r"]
}
7.func SplitN(s, sep string, n int) []string,這個是切割字符串的時刻本身界說長度,假如sep為空,那末每個字符都朋分
import (
"fmt"
"strings"
)
func main() {
fmt.Println(strings.SplitN("a,b,c", ",", 2)) //[a b,c]
}