如何将字符串数组转换为逗号分隔的字符串?

时间:2022-06-04 20:48:08

I have an array:

我有一个数组:

array = ["10", "20", "50", "99"]

and want to convert it into a simple comma-separated string list like this:

想要将它转换成一个简单的逗号分隔的字符串列表,如下所示:

"10", "20", "50", "99"

4 个解决方案

#1


74  

["10", "20", "50","99"].map(&:inspect).join(', ') # => '"10", "20", "50", "99"'

#2


104  

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.

用于保持引号和空格:数组。映射项{ | | % Q { " # {项} " } }。加入(" ")这将打印“\”10 20 \”,\“\”,\“\”,\“99 \”。如果问题确实需要一个字符串,则需要转义引号。

Documentation on the %Q: string literals.

关于%Q:字符串文字的文档。

You could use inspect as suggested in another answer, I'd say that's personal preference. I wouldn't, go look at the source code for that and choose for yourself.

你可以用另一个答案里的检查,我想这是我个人的偏好。我不会,去看看源代码然后自己选择。

Useful aside: array.to_sentence will give you a "1, 2, 3 and 4" style output, which can be nice!

有用的旁白:数组。to_sentence会给你一个“1、2、3和4”的样式输出,这很不错!

#3


16  

Here:

在这里:

array.map {|str| "\"#{str}\""}.join(',')

#4


9  

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 CSV then to roll your own.

最好使用stdlib类CSV,然后使用自己的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.

地图。如果这种编码不需要关心嵌入的分隔符,或者只用于某些内部表示,那么连接解决方案就足够了,但是如果生成数据与其他程序交换(期望逗号分隔值(CSV)作为一般理解的表示),它们就会失败。

#1


74  

["10", "20", "50","99"].map(&:inspect).join(', ') # => '"10", "20", "50", "99"'

#2


104  

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.

用于保持引号和空格:数组。映射项{ | | % Q { " # {项} " } }。加入(" ")这将打印“\”10 20 \”,\“\”,\“\”,\“99 \”。如果问题确实需要一个字符串,则需要转义引号。

Documentation on the %Q: string literals.

关于%Q:字符串文字的文档。

You could use inspect as suggested in another answer, I'd say that's personal preference. I wouldn't, go look at the source code for that and choose for yourself.

你可以用另一个答案里的检查,我想这是我个人的偏好。我不会,去看看源代码然后自己选择。

Useful aside: array.to_sentence will give you a "1, 2, 3 and 4" style output, which can be nice!

有用的旁白:数组。to_sentence会给你一个“1、2、3和4”的样式输出,这很不错!

#3


16  

Here:

在这里:

array.map {|str| "\"#{str}\""}.join(',')

#4


9  

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 CSV then to roll your own.

最好使用stdlib类CSV,然后使用自己的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.

地图。如果这种编码不需要关心嵌入的分隔符,或者只用于某些内部表示,那么连接解决方案就足够了,但是如果生成数据与其他程序交换(期望逗号分隔值(CSV)作为一般理解的表示),它们就会失败。