点击运行
package main import ( "fmt" "strings" ) // Main 函数 func main() { // 初始化字符串 str1 := "Welcome, to the, online portal, of jiyik.com" str2 := "My dog name is Dollar" str3 := "I like to play Ludo" // 显示字符串 fmt.Println("String 1: ", str1) fmt.Println("String 2: ", str2) fmt.Println("String 3: ", str3) // 使用 SplitAfterN() 方法拆分给定的字符串 res1 := strings.SplitAfterN(str1, ",", 2) res2 := strings.SplitAfterN(str2, "", 4) res3 := strings.SplitAfterN(str3, "!", 1) res4 := strings.SplitAfterN("", "jiyik.com, jiyik", 3) // 显示结果 fmt.Println("\nResult 1: ", res1) fmt.Println("Result 2: ", res2) fmt.Println("Result 3: ", res3) fmt.Println("Result 4: ", res4) }
运行结果 :
正在执行...