Ruby-on-rails 如何将字符串数组转换为逗号分隔的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11313613/
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
How do I convert an array of strings into a comma-separated string?
提问by Kashiftufail
I have an array:
我有一个数组:
array = ["10", "20", "50", "99"]
And I want to convert it into a simple comma-separated string list like this:
我想把它转换成一个简单的逗号分隔的字符串列表,如下所示:
"10", "20", "50", "99"
回答by Matt
array.join(',')will almost do what you want; it will not retain the quotes around the values nor the spaces after.
array.join(',')几乎会做你想做的事;它不会保留值周围的引号,也不会保留后面的空格。
For retaining quotes and spaces: array.map{|item| %Q{"#{item}"}}.join(', ')This will print "\"10\", \"20\", \"50\", \"99\"". The escaped quotes are necessary assuming the question does in fact call for a single string.
为了保留引号和空格:array.map{|item| %Q{"#{item}"}}.join(', ')这将打印"\"10\", \"20\", \"50\", \"99\"". 假设问题实际上需要单个字符串,则转义引号是必要的。
Documentation on the %Q: string literals.
关于%Q:字符串文字的文档。
You coulduse inspectas suggested in another answer, I'd say that's personal preference. I wouldn't, go look at the source codefor that and choose for yourself.
您可以inspect按照另一个答案中的建议使用,我会说这是个人喜好。我不会,请查看源代码并自己选择。
Useful aside: array.to_sentencewill give you a "1, 2, 3 and 4" style output, which can be nice!
除了有用:array.to_sentence会给你一个“1、2、3和4”风格的输出,这很好!
回答by sczizzo
["10", "20", "50","99"].map(&:inspect).join(', ') # => '"10", "20", "50", "99"'
回答by Linuxios
Here:
这里:
array.map {|str| "\"#{str}\""}.join(',')
回答by dbenhur
Several answers have offered solutions using #map, #inspect, #join. All of them fail to get certain details of CSV encoding correct for edge cases involving embedded commas and/or string delimiters in the elements.
几个答案提供了使用#map, #inspect, 的解决方案#join。对于涉及元素中嵌入逗号和/或字符串分隔符的边缘情况,所有这些都无法获得正确的 CSV 编码的某些细节。
It's probably a better idea to use the stdlib class CSVthen to roll your own.
使用 stdlib 类CSV然后自己滚动可能是一个更好的主意。
irb> require 'csv'
=> true
irb> a = [10,'1,234','J.R. "Bob" Dobbs',3.14159]
=> [10, "1,234", "J.R. \"Bob\" Dobbs", 3.14159]
irb> puts a.to_csv
10,"1,234","J.R. ""Bob"" Dobbs",3.14159
The map.join solutions are sufficient if this encoding doesn't need to care about embedded delimiters, or is intended for some internal representation only, but they will fail if generating data for exchange with other programs that expect Comma Separated Values (CSV) as a generally understood representation.
如果此编码不需要关心嵌入的分隔符,或者仅用于某些内部表示,则 map.join 解决方案就足够了,但是如果生成数据以与其他期望逗号分隔值 (CSV) 的程序交换,则它们将失败一种普遍理解的表示。
回答by Michael Taylor
The simplest solution is to use the built in ".to_sentence" method.
最简单的解决方案是使用内置的“.to_sentence”方法。
So
所以
["fred", "john", "amy"].to_sentence outputs "fred, john, and amy"
["fred", "john", "amy"].to_sentence 输出 "fred, john, and amy"
回答by Linearza
This is a slightly alternative solution, particularly handy if you need to convert an array with double quoted strings to a single quoted list (for say SQL queries):
这是一个稍微替代的解决方案,如果您需要将带有双引号字符串的数组转换为单引号列表(例如 SQL 查询),则特别方便:
"'#{["John Oliver", "Sam Tom"].join("','")}'"
to
到
'John Oliver', 'Sam Tom'
Attribution: https://alok-anand-ror.blogspot.com/2014/04/ruby-join-array-elements-with-single.html
归属:https: //alok-anand-ror.blogspot.com/2014/04/ruby-join-array-elements-with-single.html

