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

时间:2020-12-24 16:48:50

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!

所以“新学校”会变成“新学校”,但是“颜色”会变成“”,什么都没有!

8 个解决方案

#1


33  

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.

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

我认为如果你使用tr而不使用惊叹号,你会得到正确的结果。

#2


74  

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"

#3


35  

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.

如果您感兴趣的是在snake案例中获得一个字符串,那么建议的解决方案并不是很有效,因为您可能会得到连接下划线和开始/结尾下划线。

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提供的字符串方法

#4


7  

Old question, but...

老问题,但是……

For all whitespace 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:

可以修改regex以满足您的需要。例句:

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

#5


6  

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

Note: No "!"

注意:没有“!”

#6


3  

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

如果您正在使用rails 5或更高版本,那么您也可以使用相同的功能

str.parameterize(separator: '_')

#7


1  

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

您也可以使用str.gsub(“”,“_”)

#8


1  

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

=> "foo_bar"

#1


33  

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.

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

我认为如果你使用tr而不使用惊叹号,你会得到正确的结果。

#2


74  

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"

#3


35  

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.

如果您感兴趣的是在snake案例中获得一个字符串,那么建议的解决方案并不是很有效,因为您可能会得到连接下划线和开始/结尾下划线。

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提供的字符串方法

#4


7  

Old question, but...

老问题,但是……

For all whitespace 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:

可以修改regex以满足您的需要。例句:

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

#5


6  

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

Note: No "!"

注意:没有“!”

#6


3  

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

如果您正在使用rails 5或更高版本,那么您也可以使用相同的功能

str.parameterize(separator: '_')

#7


1  

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

您也可以使用str.gsub(“”,“_”)

#8


1  

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

=> "foo_bar"