aboutsummaryrefslogtreecommitdiff
path: root/src/strings/example_test.go
diff options
context:
space:
mode:
authorcuishuang <imcusg@gmail.com>2025-02-07 17:11:36 +0800
committerGopher Robot <gobot@golang.org>2025-02-10 21:47:04 -0800
commit7c1a4134b4191e87ae05d07343329e83aada6132 (patch)
tree2123bf7d76b92521f49ad951f2bce4d298ca0749 /src/strings/example_test.go
parent371e83cd7b309988bbe6b1bc7d0bd72aff52aa08 (diff)
downloadgo-7c1a4134b4191e87ae05d07343329e83aada6132.tar.xz
strings: add examples for Lines, SplitSeq, SplitAfterSeq, FieldsSeq and FieldsFuncSeq
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>
Diffstat (limited to 'src/strings/example_test.go')
-rw-r--r--src/strings/example_test.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/strings/example_test.go b/src/strings/example_test.go
index 08efcbf68f..da95d1e58e 100644
--- a/src/strings/example_test.go
+++ b/src/strings/example_test.go
@@ -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"
+}