iancoleman/strcase
GitHub: iancoleman/strcase
一个 Go 语言字符串命名格式转换库,支持在各种大小写风格之间灵活互转并提供自定义缩略词配置。
Stars: 1145 | Forks: 116
# strcase
[](http://godoc.org/github.com/iancoleman/strcase)
[](https://travis-ci.com/iancoleman/strcase)
[](http://gocover.io/github.com/iancoleman/strcase)
[](https://goreportcard.com/report/github.com/iancoleman/strcase)
strcase 是一个 go 包,用于将字符串转换为各种大小写格式(例如 [snake case](https://en.wikipedia.org/wiki/Snake_case) 或 [camel case](https://en.wikipedia.org/wiki/CamelCase)),请参阅下方的完整转换表。
## 示例
```
s := "AnyKind of_string"
```
| 函数 | 结果 |
|-------------------------------------------|----------------------|
| `ToSnake(s)` | `any_kind_of_string` |
| `ToSnakeWithIgnore(s, '.')` | `any_kind.of_string` |
| `ToScreamingSnake(s)` | `ANY_KIND_OF_STRING` |
| `ToKebab(s)` | `any-kind-of-string` |
| `ToScreamingKebab(s)` | `ANY-KIND-OF-STRING` |
| `ToDelimited(s, '.')` | `any.kind.of.string` |
| `ToScreamingDelimited(s, '.', '', true)` | `ANY.KIND.OF.STRING` |
| `ToScreamingDelimited(s, '.', ' ', true)` | `ANY.KIND OF.STRING` |
| `ToCamel(s)` | `AnyKindOfString` |
| `ToLowerCamel(s)` | `anyKindOfString` |
## 安装
```
go get -u github.com/iancoleman/strcase
```
## ToCamel 和 ToLowerCamel 的自定义首字母缩略词
文本中经常会包含特定的首字母缩略词,你需要以特定的方式处理它们。
开箱即用的 `strcase` 会将字符串 "ID" 处理为 "Id" 或 "id",但无法满足实际情况中的所有情况。
要在全局配置你的自定义首字母缩略词,你可以在运行任何转换之前使用以下代码
```
import (
"github.com/iancoleman/strcase"
)
func init() {
// results in "Api" using ToCamel("API")
// results in "api" using ToLowerCamel("API")
strcase.ConfigureAcronym("API", "api")
// results in "PostgreSQL" using ToCamel("PostgreSQL")
// results in "postgreSQL" using ToLowerCamel("PostgreSQL")
strcase.ConfigureAcronym("PostgreSQL", "PostgreSQL")
}
```
标签:EVTX分析, Go, Ruby工具, SOC Prime, 字符串处理, 开发工具, 日志审计, 格式转换, 第三方库