golang strings.Split函数

	email := "[email protected]"
	emailS := strings.Split(email, "@")
	fmt.Println(emailS) //[abc a.com]

	s := strings.Split("abc,abc", "")
	fmt.Println("empty seperator ", s, len(s)) // [a b c , a b c] 7
	s = strings.Split("", "")
	fmt.Println("empty && empty ", s, len(s)) // [] 0

	s = strings.Split("", ",")
	fmt.Println("empty && not empty seperator ", s, len(s)) // [] 1   注意len是1,不是0

	s = strings.Split("abc,abc", ",") // [abc abc] 2
	fmt.Println(s, len(s))
	s = strings.Split("abc,abc", "|") // [abc,abc] 1
	fmt.Println("not contain seperator ", s, len(s))

	fmt.Println(len(""), len([]string{""})) //0 1
	//str := ""
	//fmt.Println(str[0]) //panic: runtime error: index out of range [0] with length 0
	
	// 取某一位的值
	str := "abc"
	fmt.Println(str[0])

	str2 := "abc中午"
	fmt.Printf("%v,%v,%v,%c", str2[0], str2[3], []byte(str2), []rune(str2)[3])

php中explode对空字符串的分割,与go一致,不支持空分隔符

    //explode ($delimiter, $string, $limit = null) 分隔符是第一个参数
    var_dump(explode('','abc'));//分割符是空,返回Warning + bool false
    var_dump(explode(',',''));//字符串是空,返回array(0=>"")

你可能感兴趣的:(golang)