Help us shape the future of Go! Take the 2026 Go Developer Survey

stringx

package
v0.15.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 10 Imported by: 8

Documentation

Overview

Package stringx 提供常用的字符串操作工具函数。

包含字符串查找、过滤、截取、反转、大小写转换、多模式替换 (Replacer trie 最长匹配 / NewMask 敏感词遮罩)等功能。 所有函数均安全处理 UTF-8 编码的字符串。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidStartPosition indicates the start position is invalid.
	ErrInvalidStartPosition = errors.New("start position is invalid")
	// ErrInvalidStopPosition indicates the stop position is invalid.
	ErrInvalidStopPosition = errors.New("stop position is invalid")
	// ErrInvalidRange indicates the substring range is inverted (start > stop).
	ErrInvalidRange = errors.New("substring range is inverted")
)

Functions

func Contains

func Contains(list []string, str string) bool

Contains 检查 str 是否在 list 中。

func ContainsAny

func ContainsAny(text string, keywords ...string) bool

ContainsAny 检查 text 中是否包含 keywords 中的任意一个关键词(不区分大小写)。

用法:

stringx.ContainsAny("Security vulnerability found", "security", "vuln") // true
stringx.ContainsAny("normal text", "critical", "major")                 // false

func Filter

func Filter(s string, filter func(r rune) bool) string

Filter 从 s 中过滤掉满足 filter 条件的字符。

func FirstN

func FirstN(s string, n int, ellipsis ...string) string

FirstN 返回 s 的前 n 个字符,可选添加省略号。

func FormatTemplate

func FormatTemplate(template string, vars map[string]any) string

FormatTemplate 用 vars 替换 template 里的 {key} 占位符。

模板语法: `{key}` 形式,key 在 vars 里查找。

  • 未找到的 key 保留原 `{key}` 不替换(避免静默替换空字符串导致歧义)
  • 嵌套 key 不支持(只 1 层)
  • 业务场景: 错误信息模板("user {user_id} not found in tenant {tenant_id}") SQL 模板("SELECT * FROM users WHERE id = {id}"),log 模板

用法:

msg := stringx.FormatTemplate("user {user_id} not found", map[string]any{
    "user_id": 42,
})
// → "user 42 not found"

注意: 这个函数不转义;如果业务需要把用户输入嵌入 SQL/HTML,先做 业务自己的转义/参数化(否则是注入漏洞)。

func GenerateID

func GenerateID() string

GenerateID 生成基于时间戳的唯一 ID,格式: 20060102150405-<8位hex>。 适用于非严格唯一性要求的场景(如日志关联、临时标识)。 如需全局唯一,请使用 uuid。

func HasEmpty

func HasEmpty(args ...string) bool

HasEmpty 检查 args 中是否存在空字符串。

func IndentText

func IndentText(text, prefix string) string

IndentText 为多行文本的第二行及之后每行添加缩进前缀。 第一行不缩进,适用于续行对齐场景。

用法:

stringx.IndentText("line1\nline2\nline3", "  ")
// => "line1\n  line2\n  line3"

func Join

func Join(sep byte, elem ...string) string

Join 将多个字符串用 sep 连接,忽略空字符串。

func JoinByComma

func JoinByComma(str []string) string

JoinByComma 将字符串切片用逗号连接。

func JoinWith

func JoinWith(elem []string, sep string) string

JoinWith 将字符串切片用指定分隔符连接。

func NotEmpty

func NotEmpty(args ...string) bool

NotEmpty 检查 args 中所有字符串是否都不为空。

func Pluralize

func Pluralize(count int, word string) string

Pluralize 返回英文单词的复数形式。 规则:

  • count == 1 → "1 word"
  • 以 "y" 结尾且前一个字符为辅音 → 去 "y" 加 "ies" (story → stories)
  • 以 "s", "sh", "ch", "x", "z" 结尾 → 加 "es" (box → boxes)
  • 其他 → 加 "s" (file → files)

用法:

stringx.Pluralize(3, "file")   // "3 files"
stringx.Pluralize(1, "file")   // "1 file"
stringx.Pluralize(5, "story")  // "5 stories"

func Remove

func Remove(list []string, strs ...string) []string

Remove 从 strings 中移除指定的 strs。

func Reverse

func Reverse(s string) string

Reverse 反转字符串。

func SplitByComma

func SplitByComma(str string) []string

SplitByComma 将字符串按逗号分割。

func SplitWith

func SplitWith(str string, sep []string) []string

SplitWith 将字符串按指定分隔符列表分割后返回字段切片。

func Substr

func Substr(str string, start, stop int) (string, error)

Substr 返回 [start, stop) 范围内的子字符串,支持 UTF-8。

func TakeOne

func TakeOne(valid, or string) string

TakeOne 返回非空的字符串,如果 valid 非空则返回 valid,否则返回 or。

func TakeWithPriority

func TakeWithPriority(fns ...func() string) string

TakeWithPriority 返回第一个非空的函数返回值。

func ToCamelCase

func ToCamelCase(s string) string

ToCamelCase 将字符串首字母转为小写。

func TruncBytes

func TruncBytes(s string, budget int) (string, bool)

TruncBytes 按字节预算截断(rune 对齐:预算落点切进多字节字符时回退到 RuneStart 边界,不产生非法 UTF-8 尾字节)。第二个返回值 = 是否真发生了 截断。budget<0 视为非法上限,按"全部截断"处理(返回空串),不 panic。

func TruncEllipsis

func TruncEllipsis(s string, n int) string

TruncEllipsis 截断并追加省略号(列表/标题/事件载荷展示面统一语义)。 n<=0 返回省略号;无需截断时原样返回。

func TruncRunes

func TruncRunes(s string, n int) (string, bool)

TruncRunes 按 rune 截断,避免多字节字符被腰斩产生非法 UTF-8。 第二个返回值 = 是否真发生了截断。n<0 视为非法上限,按"全部截断"处理 (返回空串),不 panic。

func Union

func Union(first, second []string) []string

Union 合并两个字符串切片并去重。

Types

type Replacer added in v0.7.0

type Replacer struct {
	// contains filtered or unexported fields
}

Replacer 多模式字符串替换器。 与 strings.Replacer 的差异:按 rune 对齐匹配(不撕裂 UTF-8), 且同一起点恒取最长匹配。

func NewMask added in v0.7.0

func NewMask(words []string, mask rune) (*Replacer, error)

NewMask 创建遮罩替换器:把 words 中每个词替换为等 rune 数的 mask 串。 用于敏感词遮罩场景,是 NewReplacer 的薄壳。

func NewReplacer added in v0.7.0

func NewReplacer(patterns, repls []string) (*Replacer, error)

NewReplacer 创建多模式替换器:patterns[i] 命中的子串替换为 repls[i]。 两者长度必须一致;空 pattern 或 nil patterns(不替换任何词)的处理见 返回值。同名 pattern 后写覆盖先写。

func (*Replacer) Replace added in v0.7.0

func (r *Replacer) Replace(text string) string

Replace 按 trie 最长匹配扫描 text 并替换。 按 rune 边界迭代:未命中的 rune 原字节保留(非法 UTF-8 序列不被 改写为 U+FFFD),命中的词整体替换。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL