将数组中的一个字符串元素转换为同一数组中的多个元素

时间:2021-05-10 13:44:10

I have the following array:

我有以下数组:

info = ["Thursday, July 19, 2012", 
        " \n    4:00 PM to 6:00 PM (CT) \n                  happy hour with company\n                  company100 W. Main St.Suite 5Chicago, IL 60602"]

I'm trying to convert info[1] from a single element into multiple elements in the info array. For instance, I'd like to divide info[1] into three elements (hours, event name/description, and address). Any idea how to begin doing this?

我正在尝试将info [1]从单个元素转换为info数组中的多个元素。例如,我想将info [1]分为三个元素(小时,事件名称/描述和地址)。知道如何开始这样做吗?

1 个解决方案

#1


2  

How about

parts = info[1].split("\n").map(&:strip).reject(&:empty?)

If you want to put it back in, you could do something like

如果你想把它放回去,你可以做点什么

info[1] = parts
info.flatten!

#1


2  

How about

parts = info[1].split("\n").map(&:strip).reject(&:empty?)

If you want to put it back in, you could do something like

如果你想把它放回去,你可以做点什么

info[1] = parts
info.flatten!