如何用ruby安全地用下划线替换所有空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7547065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:02:31  来源:igfitidea点击:

how to safely replace all whitespaces with underscores with ruby?

ruby-on-railsrubyruby-on-rails-3

提问by rugbert

This works for any strings that have whitespaces in them

这适用于任何包含空格的字符串

str.downcase.tr!(" ", "_")

but strings that dont have whitespaces just get deleted

但是没有空格的字符串会被删除

So "New School" would change into "new_school" but "color" would be "", nothing!

所以“New School”会变成“new_school”但“color”会变成“”,什么都没有!

采纳答案by rwilliams

The docs for tr!say

tr的文档

Translates str in place, using the same rules as String#tr. Returns str, or nil if no changes were made.

使用与 String#tr 相同的规则,就地翻译 str。如果未进行任何更改,则返回 str 或 nil。

I think you'll get the correct results if you use trwithout the exclamation.

我认为如果您使用不带感叹号的tr ,您会得到正确的结果。

回答by Sampat Badhe

with space

有空间

str = "New School"
str.parameterize.underscore

=> "new_school"

without space

没有空间

str = "school"
str.parameterize.underscore

=> "school"

Edit :- also we can pass '_' as parameter to parameterize.

编辑:-我们也可以将“_”作为参数传递给参数化。

with space

有空间

str = "New School"
str.parameterize('_')

=> "new_school"

without space

没有空间

str = "school"
str.parameterize('_')

=> "school"

EDIT:

编辑

For Rails 5 and above, use str.parameterize(separator: '_')

对于 Rails 5 及更高版本,请使用 str.parameterize(separator: '_')

回答by Zack Xu

If you're interested in getting a string in snake case, then the proposed solution doesn't quite work, because you may get concatenated underscores and starting/trailing underscores.

如果您有兴趣在蛇案例中获取字符串,那么建议的解决方案不太适用,因为您可能会得到连接的下划线和起始/尾随下划线。

For example

例如

1.9.3-p0 :010 > str= "  John   Smith Beer "
  => "  John   Smith Beer " 
1.9.3-p0 :011 > str.downcase.tr(" ", "_")
  => "__john___smith_beer_"

This solution below would work better:

下面的这个解决方案会更好:

1.9.3-p0 :010 > str= "  John   Smith Beer "
  => "  John   Smith Beer " 
1.9.3-p0 :012 > str.squish.downcase.tr(" ","_")
  => "john_smith_beer" 

squish is a String method provided by Rails

squish 是 Rails 提供的 String 方法

回答by theterminalguy

If you are using rails 5 and above you can achieve the same thing with

如果您使用的是 rails 5 及更高版本,则可以使用

str.parameterize(separator: '_')

回答by br3nt

Old question, but...

老问题,但是...

For allwhitespace you probably want something more like this:

对于所有空白,您可能想要更像这样的东西:

"hey\t there   world".gsub(/\s+/, '_') # hey_there_world

This gets tabs and new lines as well as spaces and replaces with a single _.

这将获取制表符和新行以及空格并替换为单个_.

The regex can be modified to suit your needs. E.g:

可以修改正则表达式以满足您的需要。例如:

"hey\t there   world".gsub(/\s/, '_') # hey__there___world

回答by Michael Durrant

str.downcase.tr(" ", "_")

Note: No "!"

注意:没有“!”

回答by Trip

str = "Foo Bar"
str.tr(' ','').underscore

=> "foo_bar"

回答by randomuser

You can also do str.gsub(" ", "_")

你也可以做 str.gsub(" ", "_")