golang跨语言字符串处理工具插件库xstrings的使用
Golang跨语言字符串处理工具插件库xstrings的使用
xstrings是一个Go语言字符串处理库,提供了许多其他语言中常见但Go标准库strings中缺失的字符串处理函数。
安装
使用go get命令安装xstrings库:
go get github.com/huandu/xstrings
功能列表
xstrings提供了许多实用的字符串处理函数,下面是主要函数及其在其他语言中的对应函数:
函数 | 其他语言中的对应函数 |
---|---|
Center | Python的str.center, Ruby的String#center |
Count | Ruby的String#count |
Delete | Ruby的String#delete |
ExpandTabs | Python的str.expandtabs |
FirstRuneToLower | PHP/Perl的lcfirst |
FirstRuneToUpper | Ruby的String#capitalize, PHP/Perl的ucfirst |
Insert | Ruby的String#insert |
LastPartition | Python的str.rpartition, Ruby的String#rpartition |
LeftJustify | Python的str.ljust, Ruby的String#ljust |
Len | PHP的mb_strlen |
Partition | Python的str.partition, Ruby的String#partition |
Reverse | Ruby的String#reverse, PHP的strrev, Perl的reverse |
RightJustify | Python的str.rjust, Ruby的String#rjust |
Scrub | Ruby的String#scrub |
Shuffle | PHP的str_shuffle |
Slice | PHP的mb_substr |
Squeeze | Ruby的String#squeeze |
Successor | Ruby的String#succ或String#next |
SwapCase | Python的str.swapcase, Ruby的String#swapcase |
ToCamelCase | RoR的String#camelize |
ToSnakeCase | RoR的String#underscore |
Translate | Python的str.translate, Ruby的String#tr, PHP的strtr, Perl的tr/// |
Width | PHP的mb_strwidth |
WordCount | PHP的str_word_count |
使用示例
下面是一些常用函数的示例代码:
package main
import (
"fmt"
"github.com/huandu/xstrings"
)
func main() {
// Center - 将字符串居中,用空格填充到指定宽度
fmt.Println(xstrings.Center("hello", 10)) // " hello "
// Reverse - 反转字符串
fmt.Println(xstrings.Reverse("hello")) // "olleh"
// ToCamelCase - 将字符串转换为驼峰命名
fmt.Println(xstrings.ToCamelCase("hello_world")) // "helloWorld"
// ToSnakeCase - 将字符串转换为蛇形命名
fmt.Println(xstrings.ToSnakeCase("HelloWorld")) // "hello_world"
// SwapCase - 交换字符串中字母的大小写
fmt.Println(xstrings.SwapCase("Hello World")) // "hELLO wORLD"
// Shuffle - 随机打乱字符串中的字符
fmt.Println(xstrings.Shuffle("hello")) // 随机输出如"lhleo"
// Count - 计算子字符串出现的次数
fmt.Println(xstrings.Count("hello hello", "he")) // 2
// Partition - 将字符串分割为三部分(分隔符前,分隔符,分隔符后)
fmt.Println(xstrings.Partition("hello world", " ")) // ["hello", " ", "world"]
}
性能说明
xstrings中的所有函数都经过了良好的测试,并针对性能进行了精心调优,可以放心在生产环境中使用。
许可证
xstrings库采用MIT许可证,详情请查看LICENSE文件。
更多关于golang跨语言字符串处理工具插件库xstrings的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复