Files
go-easy-utils/strx/cut.go
2025-07-23 15:03:25 +08:00

22 lines
490 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package strx
import "strings"
// Cut 删除 s 中出现的 sub 字符串
// 第三个参数 n 可选:
// - 省略 → 删除所有匹配(等价 strings.ReplaceAll
// - n == 1 → 只删除第一次出现
// - n > 1 → 删除前 n 次出现
func Cut(s, sub string, n ...int) string {
if len(sub) == 0 || len(s) == 0 {
return s // nothing to do
}
// 默认: 删除所有
count := -1
if len(n) > 0 {
count = n[0]
}
return strings.Replace(s, sub, "", count)
}