]> Cypherpunks repositories - gostls13.git/commitdiff
strings: add examples for Lines, SplitSeq, SplitAfterSeq, FieldsSeq and FieldsFuncSeq
authorcuishuang <imcusg@gmail.com>
Fri, 7 Feb 2025 09:11:36 +0000 (17:11 +0800)
committerGopher Robot <gobot@golang.org>
Tue, 11 Feb 2025 05:47:04 +0000 (21:47 -0800)
Change-Id: I1e5085ff2ed7f3d75ac3dc34ab72be6b55729fb7
Reviewed-on: https://go-review.googlesource.com/c/go/+/647575
Auto-Submit: Ian Lance Taylor <iant@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/strings/example_test.go

index 08efcbf68ff0f6a03325b8cb91bc998f393d8100..da95d1e58e6499e994368c53c9aa28ff760c5be7 100644 (file)
@@ -458,3 +458,93 @@ func ExampleToValidUTF8() {
        // abc
        // abc
 }
+
+func ExampleLines() {
+       text := "Hello\nWorld\nGo Programming\n"
+       for line := range strings.Lines(text) {
+               fmt.Printf("%q\n", line)
+       }
+
+       // Output:
+       // "Hello\n"
+       // "World\n"
+       // "Go Programming\n"
+}
+
+func ExampleSplitSeq() {
+       s := "a,b,c,d"
+       for part := range strings.SplitSeq(s, ",") {
+               fmt.Printf("%q\n", part)
+       }
+
+       // Output:
+       // "a"
+       // "b"
+       // "c"
+       // "d"
+}
+
+func ExampleSplitAfterSeq() {
+       s := "a,b,c,d"
+       for part := range strings.SplitAfterSeq(s, ",") {
+               fmt.Printf("%q\n", part)
+       }
+
+       // Output:
+       // "a,"
+       // "b,"
+       // "c,"
+       // "d"
+}
+
+func ExampleFieldsSeq() {
+       text := "The quick brown fox"
+       fmt.Println("Split string into fields:")
+       for word := range strings.FieldsSeq(text) {
+               fmt.Printf("%q\n", word)
+       }
+
+       textWithSpaces := "  lots   of   spaces  "
+       fmt.Println("\nSplit string with multiple spaces:")
+       for word := range strings.FieldsSeq(textWithSpaces) {
+               fmt.Printf("%q\n", word)
+       }
+
+       // Output:
+       // Split string into fields:
+       // "The"
+       // "quick"
+       // "brown"
+       // "fox"
+       //
+       // Split string with multiple spaces:
+       // "lots"
+       // "of"
+       // "spaces"
+}
+
+func ExampleFieldsFuncSeq() {
+       text := "The quick brown fox"
+       fmt.Println("Split on whitespace(similar to FieldsSeq):")
+       for word := range strings.FieldsFuncSeq(text, unicode.IsSpace) {
+               fmt.Printf("%q\n", word)
+       }
+
+       mixedText := "abc123def456ghi"
+       fmt.Println("\nSplit on digits:")
+       for word := range strings.FieldsFuncSeq(mixedText, unicode.IsDigit) {
+               fmt.Printf("%q\n", word)
+       }
+
+       // Output:
+       // Split on whitespace(similar to FieldsSeq):
+       // "The"
+       // "quick"
+       // "brown"
+       // "fox"
+       //
+       // Split on digits:
+       // "abc"
+       // "def"
+       // "ghi"
+}