英文:
How to convert a string into a snake case?
问题
我有一个字符串列表:
myStrings = []string{
"MyString1",
"SomeName33",
"AaaBbbTutu",
"JaaaKooooTooo"
}
如何将它们转换为蛇形字符串?
mySnakeCaseStrings = []string{
"my_string1",
"some_name33",
"aaa_bbb_tutu",
"jaaa_koooo_tooo"
}
因为strings
包中没有专门的函数来实现这个功能。手动编写自定义代码来完成这个简单的任务将会非常繁琐。
英文:
I have a list of strings:
myStrings = []string{
"MyString1",
"SomeName33",
"AaaBbbTutu",
"JaaaKooooTooo"
}
How do I convert them into case snake strings?
mySnakeCaseStrings = []string{
"my_string1",
"some_name33",
"aaa_bbb_tutu",
"jaaa_koooo_tooo"
}
For there's no special function in strings
for this. And doing it manually, by custom code, would be a lot of work for such a simple task.
答案1
得分: 1
这就是iancoleman/strcase
这样的专用库的作用:
它有一个名为ToSnake(s)
的函数,可以将s := "AnyKind of_string"
转换为any_kind_of_string
。
英文:
That is what a dedicated library like iancoleman/strcase
is for:
It has a function ToSnake(s)
which will produce any_kind_of_string
from s := "AnyKind of_string"
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论