Skip to content

string

多行文本

使用 ```

SplitN

go
package main

import (
	"fmt"
	"strings"
)

func main() {
	s := "a,b,c,d"
	sep := ","

	// n=1:拆分0次,切片长度1
	fmt.Println("n=1:", strings.SplitN(s, sep, 1)) // ["a,b,c,d"]

	// n=2:拆分1次,切片长度2
	fmt.Println("n=2:", strings.SplitN(s, sep, 2)) // ["a" "b,c,d"]

	// n=3:拆分2次,切片长度3
	fmt.Println("n=3:", strings.SplitN(s, sep, 3)) // ["a" "b" "c,d"]
}