mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 04:46:22 +08:00
35 lines
774 B
Go
35 lines
774 B
Go
// strings.go
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
str := "The quick brown fox jumps over the lazy dog"
|
|
sl := strings.Fields(str)
|
|
fmt.Printf("Splitted in slice: %v\n", sl)
|
|
for _, val := range sl {
|
|
fmt.Printf("%s - ", val)
|
|
}
|
|
fmt.Println()
|
|
str2 := "GO1|The ABC of Go|25"
|
|
sl2 := strings.Split(str2, "|")
|
|
fmt.Printf("Splitted in slice: %v\n", sl2)
|
|
for _, val := range sl2 {
|
|
fmt.Printf("%s - ", val)
|
|
}
|
|
fmt.Println()
|
|
str3 := strings.Join(sl2, ";")
|
|
fmt.Printf("sl2 joined by ;: %s\n", str3)
|
|
}
|
|
|
|
/* Output:
|
|
Splitted in slice: [The quick brown fox jumps over the lazy dog]
|
|
The - quick - brown - fox - jumps - over - the - lazy - dog -
|
|
Splitted in slice: [GO1 The ABC of Go 25]
|
|
GO1 - The ABC of Go - 25 -
|
|
sl2 joined by ;: GO1;The ABC of Go;25
|
|
*/
|