Ruby 将数组合并为一个字符串 [英] Ruby combining an array into one string

查看:33
本文介绍了Ruby 将数组合并为一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Ruby 中,有没有办法将所有数组元素组合成一个字符串?

In Ruby is there a way to combine all array elements into one string?

示例数组:

@arr = ['<p>Hello World</p>', '<p>This is a test</p>']

示例输出:

<p>Hello World</p><p>This is a test</p>

推荐答案

使用 Array#join 方法(join 的参数是在字符串之间插入的内容 -在这种情况下是一个空格):

Use the Array#join method (the argument to join is what to insert between the strings - in this case a space):

@arr.join(" ")

这篇关于Ruby 将数组合并为一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆