【问题标题】:rails split and capitalize words in [ ]rails 拆分 [ ] 中的单词并将其大写
【发布时间】:2015-05-13 17:02:57
【问题描述】:

我正在尝试将具有以下格式的单词大写:

america [asia] europe

我想让它看起来像这样:

America [Asia] Europe

我应该在 .split 选项中输入什么正则表达式?

【问题讨论】:

  • 您的数据没有意义。 america [asia] europe 是什么?一个数组,一个字符串?
  • 强制性笑话:你有问题。您决定使用正则表达式。现在你有两个问题。说真的:这是字符串的唯一形式,还是括号可以移动,例如“[america] asia europe”、“america [asia] europe”或“america asia [europe]”?我会在在线正则表达式测试器中尝试一些东西。

标签: ruby-on-rails split delimiter capitalize


【解决方案1】:

如果你的字符串是america [asia] europe,那么就不需要regex。而是使用 Rails 中的内置辅助方法。方法titleize 就是针对这种事情的。

➜  test_app git:(master) ✗ rails c
Loading development environment (Rails 4.2.0)

test_app :001 > t = 'america [asia] europe'
 => "america [asia] europe" 

test_app :002 > t.titleize
 => "America [Asia] Europe" 

【讨论】:

    猜你喜欢
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多