如何将字符串转换为蛇形命名法(snake case)?

huangapple go评论109阅读模式
英文:

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".

huangapple
  • 本文由 发表于 2021年9月29日 14:12:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/69371239.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定