Skip to content

Commit 7c1a413

Browse files
cuishuanggopherbot
authored andcommitted
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>
1 parent 371e83c commit 7c1a413

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

src/strings/example_test.go

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,3 +458,93 @@ func ExampleToValidUTF8() {
458458
// abc
459459
// abc
460460
}
461+
462+
func ExampleLines() {
463+
text := "Hello\nWorld\nGo Programming\n"
464+
for line := range strings.Lines(text) {
465+
fmt.Printf("%q\n", line)
466+
}
467+
468+
// Output:
469+
// "Hello\n"
470+
// "World\n"
471+
// "Go Programming\n"
472+
}
473+
474+
func ExampleSplitSeq() {
475+
s := "a,b,c,d"
476+
for part := range strings.SplitSeq(s, ",") {
477+
fmt.Printf("%q\n", part)
478+
}
479+
480+
// Output:
481+
// "a"
482+
// "b"
483+
// "c"
484+
// "d"
485+
}
486+
487+
func ExampleSplitAfterSeq() {
488+
s := "a,b,c,d"
489+
for part := range strings.SplitAfterSeq(s, ",") {
490+
fmt.Printf("%q\n", part)
491+
}
492+
493+
// Output:
494+
// "a,"
495+
// "b,"
496+
// "c,"
497+
// "d"
498+
}
499+
500+
func ExampleFieldsSeq() {
501+
text := "The quick brown fox"
502+
fmt.Println("Split string into fields:")
503+
for word := range strings.FieldsSeq(text) {
504+
fmt.Printf("%q\n", word)
505+
}
506+
507+
textWithSpaces := " lots of spaces "
508+
fmt.Println("\nSplit string with multiple spaces:")
509+
for word := range strings.FieldsSeq(textWithSpaces) {
510+
fmt.Printf("%q\n", word)
511+
}
512+
513+
// Output:
514+
// Split string into fields:
515+
// "The"
516+
// "quick"
517+
// "brown"
518+
// "fox"
519+
//
520+
// Split string with multiple spaces:
521+
// "lots"
522+
// "of"
523+
// "spaces"
524+
}
525+
526+
func ExampleFieldsFuncSeq() {
527+
text := "The quick brown fox"
528+
fmt.Println("Split on whitespace(similar to FieldsSeq):")
529+
for word := range strings.FieldsFuncSeq(text, unicode.IsSpace) {
530+
fmt.Printf("%q\n", word)
531+
}
532+
533+
mixedText := "abc123def456ghi"
534+
fmt.Println("\nSplit on digits:")
535+
for word := range strings.FieldsFuncSeq(mixedText, unicode.IsDigit) {
536+
fmt.Printf("%q\n", word)
537+
}
538+
539+
// Output:
540+
// Split on whitespace(similar to FieldsSeq):
541+
// "The"
542+
// "quick"
543+
// "brown"
544+
// "fox"
545+
//
546+
// Split on digits:
547+
// "abc"
548+
// "def"
549+
// "ghi"
550+
}

0 commit comments

Comments
 (0)