golang字符串处理与转换功能插件库gostrutils的使用

Go String Utilities (gostrutils) 使用指南

概述

gostrutils 是一个包含各种字符串实用功能的 Golang 库,它补充了标准库 strings 包中缺失的功能,以及其他编码包提供的功能。这个库是作者多年来在项目中积累的字符串处理函数的集合,旨在避免在每个新项目中重复造轮子。

基本特性

  • 基于 Go 的基本逻辑,使用字节作为非数字容器
  • 提供标准库缺失的字符串处理功能
  • 完全支持 UTF-8 字符串
  • 按照功能主题组织代码文件
  • helpers.go 文件包含非字符串相关的辅助函数

质量保证

该项目旨在达到接近 100% 的单元测试覆盖率,并为所有现有函数提供示例。

Go Report Card GolangCI Build Status Coverage Status GoDoc License

依赖关系

该库设计为仅依赖 Golang 标准库,以最小化依赖关系。在使用此包时不需要任何第三方包,项目的所有贡献也必须考虑这一点。

使用示例

以下是一个使用 gostrutils 的完整示例:

package main

import (
	"fmt"
	"github.com/ik5/gostrutils"
)

func main() {
	// 示例1: 检查字符串是否以某个前缀开头(忽略大小写)
	str := "Hello World"
	prefix := "hello"
	if gostrutils.StrStartsWithIgnoreCase(str, prefix) {
		fmt.Printf("'%s' starts with '%s' (case insensitive)\n", str, prefix)
	}

	// 示例2: 将字符串转换为蛇形命名法(snake_case)
	camelCase := "MyVariableName"
	snakeCase := gostrutils.CamelToSnake(camelCase)
	fmt.Printf("CamelCase: %s → SnakeCase: %s\n", camelCase, snakeCase)

	// 示例3: 检查字符串是否是有效的UTF-8
	validStr := "Hello, 世界"
	if gostrutils.IsUTF8(validStr) {
		fmt.Printf("'%s' is valid UTF-8\n", validStr)
	}

	// 示例4: 反转字符串
	original := "abcdef"
	reversed := gostrutils.Reverse(original)
	fmt.Printf("Original: %s → Reversed: %s\n", original, reversed)
}

更多功能示例

package main

import (
	"fmt"
	"github.com/ik5/gostrutils"
)

func main() {
	// 示例5: 移除字符串中的重复字符
	withDuplicates := "aabbccddeeff"
	unique := gostrutils.RemoveDuplicateChars(withDuplicates)
	fmt.Printf("With duplicates: %s → Unique: %s\n", withDuplicates, unique)

	// 示例6: 检查字符串是否是回文
	palindrome := "madam"
	if gostrutils.IsPalindrome(palindrome) {
		fmt.Printf("'%s' is a palindrome\n", palindrome)
	}

	// 示例7: 生成随机字符串
	randomStr := gostrutils.RandomString(10, gostrutils.Letters)
	fmt.Printf("Random string: %s\n", randomStr)

	// 示例8: 转换字符串为驼峰命名法(CamelCase)
	snakeStr := "my_variable_name"
	camelStr := gostrutils.SnakeToCamel(snakeStr)
	fmt.Printf("SnakeCase: %s → CamelCase: %s\n", snakeStr, camelStr)
}

许可证

该项目采用 MIT 许可证发布。


更多关于golang字符串处理与转换功能插件库gostrutils的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang字符串处理与转换功能插件库gostrutils的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


gostrutils - Go字符串处理与转换功能库

gostrutils是一个强大的Go语言字符串处理工具库,提供了丰富的字符串操作功能,包括转换、比较、验证、格式化等。下面我将介绍其主要功能和使用方法。

安装

go get github.com/thewinds/gostrutils

主要功能与示例

1. 字符串转换

import "github.com/thewinds/gostrutils"

// 转换为驼峰命名
fmt.Println(gostrutils.ToCamelCase("hello_world")) // "HelloWorld"

// 转换为蛇形命名
fmt.Println(gostrutils.ToSnakeCase("HelloWorld")) // "hello_world"

// 转换为短横线命名
fmt.Println(gostrutils.ToKebabCase("HelloWorld")) // "hello-world"

2. 字符串验证

// 检查是否为纯字母
fmt.Println(gostrutils.IsAlpha("abc")) // true
fmt.Println(gostrutils.IsAlpha("abc123")) // false

// 检查是否为字母或数字
fmt.Println(gostrutils.IsAlphanumeric("abc123")) // true

// 检查是否为数字
fmt.Println(gostrutils.IsNumeric("123.45")) // true

3. 字符串操作

// 随机字符串生成
randomStr := gostrutils.RandomStr(10, gostrutils.Letters)
fmt.Println(randomStr) // 类似 "aBcDeFgHiJ"

// 字符串反转
fmt.Println(gostrutils.Reverse("hello")) // "olleh"

// 截取字符串
fmt.Println(gostrutils.Substr("hello world", 0, 5)) // "hello"

4. 字符串格式化

// 首字母大写
fmt.Println(gostrutils.Ucfirst("hello")) // "Hello"

// 首字母小写
fmt.Println(gostrutils.Lcfirst("Hello")) // "hello"

// 单词首字母大写
fmt.Println(gostrutils.Ucwords("hello world")) // "Hello World"

5. 字符串比较

// 相似度比较
similarity := gostrutils.Similarity("kitten", "sitting")
fmt.Println(similarity) // 0.6153846153846154 (0-1之间的相似度)

// 模糊匹配
fmt.Println(gostrutils.FuzzyMatch("hello", "hlo")) // true

6. 编码转换

// Base64编码解码
encoded := gostrutils.Base64Encode("hello")
decoded, _ := gostrutils.Base64Decode(encoded)
fmt.Println(decoded) // "hello"

// URL编码解码
urlEncoded := gostrutils.URLEncode("hello world")
urlDecoded, _ := gostrutils.URLDecode(urlEncoded)
fmt.Println(urlDecoded) // "hello world"

7. 字符串处理工具

// 去除所有空白字符
fmt.Println(gostrutils.TrimAll(" hello \n world \t ")) // "helloworld"

// 生成UUID
uuid := gostrutils.UUID()
fmt.Println(uuid) // 类似 "6ba7b810-9dad-11d1-80b4-00c04fd430c8"

// 生成MD5/SHA1等哈希
fmt.Println(gostrutils.MD5("hello")) // "5d41402abc4b2a76b9719d911017c592"

高级用法示例

自定义转换规则

// 自定义转换器
converter := gostrutils.NewCaseConverter(
    gostrutils.WithInitialisms(map[string]bool{
        "ID":  true,
        "URL": true,
    }),
)

fmt.Println(converter.ToCamelCase("user_id")) // "UserID" 而不是 "UserId"

组合多个操作

// 清理用户输入并格式化
userInput := "  Hello  World 123  "
cleaned := gostrutils.Chain(userInput).
    TrimSpace().
    RemoveNumbers().
    TitleCase().
    Value()

fmt.Println(cleaned) // "Hello World"

性能考虑

gostrutils在设计时考虑了性能因素,大多数操作都是零分配或最小化内存分配。对于高频使用的场景,建议:

  1. 重用转换器实例
  2. 对大文本使用流式处理
  3. 避免在循环中重复创建相同的正则表达式

总结

gostrutils提供了全面的字符串处理功能,可以大大简化Go开发中的字符串操作。它的API设计简洁,性能良好,适合各种规模的Go项目。

更多详细用法请参考官方文档:https://github.com/thewinds/gostrutils

回到顶部