當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。