当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


GO Cut用法及代码示例

GO语言"bytes"包中"Cut"函数的用法及代码示例。

用法:

func Cut(s, sep []byte)(before, after []byte, found bool)

在 sep 的第一个实例周围切割切片 s,返回 sep 之前和之后的文本。找到的结果报告 sep 是否出现在 s 中。如果 sep 没有出现在 s 中,cut 返回 s, nil, false。

Cut 返回原始切片 s 的切片,而不是副本。

例子:

package main

import (
    "bytes"
    "fmt"
)

func main() {
    show := func(s, sep string) {
        before, after, found := bytes.Cut([]byte(s), []byte(sep))
        fmt.Printf("Cut(%q, %q) = %q, %q, %v\n", s, sep, before, after, found)
    }
    show("Gopher", "Go")
    show("Gopher", "ph")
    show("Gopher", "er")
    show("Gopher", "Badger")
}

输出:

Cut("Gopher", "Go") = "", "pher", true
Cut("Gopher", "ph") = "Go", "er", true
Cut("Gopher", "er") = "Goph", "", true
Cut("Gopher", "Badger") = "Gopher", "", false

相关用法


注:本文由纯净天空筛选整理自golang.google.cn大神的英文原创作品 Cut。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。