Differences between revisions 3 and 4
Revision 3 as of 2023-01-08 05:56:16
Size: 1667
Comment:
Revision 4 as of 2025-10-10 14:48:41
Size: 3513
Comment: Rewrite
Deletions are marked like this. Additions are marked like this.
Line 11: Line 11:
== Clone == == Usage ==



=== Split ===

There are several similar functions for splitting.

||'''Name''' ||'''Meaning''' ||
||`Split(a, b string) []string` ||Split `a` by every instance of `b` ||
||`SplitN(a, b string, n int) []string` ||Split `a` by every instance of `b`, up to `n` times ||
||`SplitAfter(a, b string) []string` ||Split `a` after every instance of `b` ||
||`SplitAfterN(a, b string, n int) []string` ||Split `a` after every instance of `b`, up to `n` times||
||`SplitSeq(a, b string, n int) iter.Seq[string]`||Split `a` by every instance of `b` ||
||`SplitAfterSeq(a, b string) iter.Seq[string]` ||Split `a` after every instance of `b` ||

If `b` is not in `a`, the return value is a slice of length 1 containing `a`. If `b` is empty, then `a` is split between every UTF-8 sequence.

Passing `n` of -1 to `SplitN` is equivalent to `Split`.

To demonstrate the `After` variants:

{{{
strings.Split("a,b,c", ",") // ["a" "b" "c"]
strings.SplitAfter("a,b,c", ",") // ["a," "b," "c"]
}}}

The `Seq` variants are equivalent but return a single-use iterator. This can be more efficient.



=== Replace ===

There are several similar functions for replacement of substrings.

||'''Name''' ||'''Meaning''' ||
||`Replace(a, b, c string, n int) string`||Returns `s` with all instances of `b` replaced with `c`, up to `n` times||
||`ReplaceAll(a, b, c string) string` ||Returns `s` with all instances of `b` replaced with `c` ||

A `Replacer` is used to perform more complicated replacements.

{{{
message := "What??? IMPOSSIBLE!"
replacer := strings.NewReplacer("!","", "?","")
replacer.Replace(strings.ToLower(message)) // "what impossible"
}}}



=== Builder ===

A `Builder` is used to more efficiently compose a large string.

{{{
import (
    "strings"
    "regexp"
    "bufio"
    "fmt"
    "os"
)

var Pattern = regexp.MustCompile(`PING`)

func main() {
    count := 0

    // Initialize the Builder
    var content strings.Builder

    // Initialize the Scanner using STDIN
    scanner := bufio.NewScanner(os.Stdin)

    // Loop over scanned lines
    for scanner.Scan() {
        line := scanner.Text()

        matches := Pattern.FindAllStringIndex(line, -1)

        // Build lines
        for i := len(matches)-1; i >= 0; i-- {
            line = line[:matches[i][0]] + fmt.Sprintf("[%d]", count+i) + line[matches[i][1]:]
        }
        content.WriteString(line)
        content.WriteString("\n")

        count += len(matches)
    }

    // Check for scanner errors
    if err := scanner.Err(); err != nil {
        fmt.Println(err)
    }

    // Print to STDOUT
    fmt.Printf(content.String())
}
}}}

{{{
$ cat test
Hello, this is PING your PING friend.
I am PING testing your work. PING.
PING PING PING test PING.

$ cat test | ./scan-and-build
Hello, this is [0] your [1] friend.
I am [2] testing your work. [3].
[4] [5] [6] test [7].

}}}
Line 17: Line 127:
== Compare == == See also ==
Line 19: Line 129:
----



== Contains ==

----



== ContainsAny ==

----



== ContainsRune ==

----



== Count ==

----



== Cut ==

----



== EqualFold ==

----



== Fields ==

----



== FieldsFunc ==

----



== HasPrefix ==

----



== HasSuffix ==

----



== Index ==

----



== IndexAny ==

----



== IndexByte ==

----



== IndexFunc ==

----



== IndexRune ==

----



== Join ==

----



== LastIndex ==

----



== LastIndexAny ==

----



== LastIndexByte ==

----



== LastIndexFunc ==

----



== Map ==

----



== Repeat ==

----



== Replace ==

----



== ReplaceAll ==

----



== Split ==

----



== SplitAfter ==

----



== SplitAfterN ==

----



== SplitN ==

----



== Title ==

----



== ToLower ==

----



== ToLowerSpecial ==

----



== ToTitle ==

----



== ToTitleSpecial ==

----



== ToUpper ==

----



== ToUpperSpecial ==

----



== ToValidUTF8 ==

----



== Trim ==

----



== TrimFunc ==

----



== TrimLeft ==

----



== TrimLeftFunc ==

----



== TrimPrefix ==

----



== TrimRight ==

----



== TrimRightFunc ==

----



== TrimSpace ==

----



== TrimSuffix ==

----



== Builder ==

----



== Reader ==

----



== Replacer ==
[[https://pkg.go.dev/strings|strings package]]

Go Strings

The strings package is used for operating on string data.


Usage

Split

There are several similar functions for splitting.

Name

Meaning

Split(a, b string) []string

Split a by every instance of b

SplitN(a, b string, n int) []string

Split a by every instance of b, up to n times

SplitAfter(a, b string) []string

Split a after every instance of b

SplitAfterN(a, b string, n int) []string

Split a after every instance of b, up to n times

SplitSeq(a, b string, n int) iter.Seq[string]

Split a by every instance of b

SplitAfterSeq(a, b string) iter.Seq[string]

Split a after every instance of b

If b is not in a, the return value is a slice of length 1 containing a. If b is empty, then a is split between every UTF-8 sequence.

Passing n of -1 to SplitN is equivalent to Split.

To demonstrate the After variants:

strings.Split("a,b,c", ",")       // ["a" "b" "c"]
strings.SplitAfter("a,b,c", ",")  // ["a," "b," "c"]

The Seq variants are equivalent but return a single-use iterator. This can be more efficient.

Replace

There are several similar functions for replacement of substrings.

Name

Meaning

Replace(a, b, c string, n int) string

Returns s with all instances of b replaced with c, up to n times

ReplaceAll(a, b, c string) string

Returns s with all instances of b replaced with c

A Replacer is used to perform more complicated replacements.

message := "What??? IMPOSSIBLE!"
replacer := strings.NewReplacer("!","", "?","")
replacer.Replace(strings.ToLower(message))       // "what impossible"

Builder

A Builder is used to more efficiently compose a large string.

import (
    "strings"
    "regexp"
    "bufio"
    "fmt"
    "os"
)

var Pattern = regexp.MustCompile(`PING`)

func main() {
    count := 0

    // Initialize the Builder
    var content strings.Builder

    // Initialize the Scanner using STDIN
    scanner := bufio.NewScanner(os.Stdin)

    // Loop over scanned lines
    for scanner.Scan() {
        line := scanner.Text()

        matches := Pattern.FindAllStringIndex(line, -1)

        // Build lines
        for i := len(matches)-1; i >= 0; i-- {
            line = line[:matches[i][0]] + fmt.Sprintf("[%d]", count+i) + line[matches[i][1]:]
        }
        content.WriteString(line)
        content.WriteString("\n")

        count += len(matches)
    }

    // Check for scanner errors
    if err := scanner.Err(); err != nil {
        fmt.Println(err)
    }

    // Print to STDOUT
    fmt.Printf(content.String())
}

$ cat test
Hello, this is PING your PING friend.
I am PING testing your work. PING.
PING PING PING test PING.

$ cat test | ./scan-and-build
Hello, this is [0] your [1] friend.
I am [2] testing your work. [3].
[4] [5] [6] test [7].


See also

strings package


CategoryRicottone

Go/Strings (last edited 2025-10-10 15:20:38 by DominicRicottone)