# How to convert a String to an Array?

**URL:** <https://www.ruby-forum.com/t/how-to-convert-a-string-to-an-array/89004>\
**Category:** Ruby\
**Created:** [March 16, 2007, 1:18pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-to-an-array/89004 "2007-03-16T13:18:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![RubyCrazy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/848f3c/32.png) [@RubyCrazy](https://www.ruby-forum.com/u/RubyCrazy)\
**Post date:** [March 16, 2007, 1:18pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-to-an-array/89004/1 "2007-03-16T13:18:10Z")

</div>

Is there a way to convert a String to an Array? I could only find the  
following -  
‘hello world’.scan(/./)

Thanks.

---

<div class="post-metadata">

**Author:** ![RubyCrazy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/848f3c/32.png) [@RubyCrazy](https://www.ruby-forum.com/u/RubyCrazy)\
**Post date:** [March 16, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-to-an-array/89004/2 "2007-03-16T13:25:11Z")

</div>

On 16/03/07, RubyCrazy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there a way to convert a String to an Array? I could only find the  
> following -  
> ‘hello world’.scan(/./)
> 
> Thanks.

irb(main):001:0\> “hello world”.split(//)  
=\> [“h”, “e”, “l”, “l”, “o”, " ", “w”, “o”, “r”, “l”, “d”]

I’m not sure if this is functionally the same or if there is some kind  
of performance boost using split instead of scan.

Farrel

---

<div class="post-metadata">

**Author:** ![RubyCrazy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/848f3c/32.png) [@RubyCrazy](https://www.ruby-forum.com/u/RubyCrazy)\
**Post date:** [March 16, 2007, 1:31pm UTC](https://www.ruby-forum.com/t/how-to-convert-a-string-to-an-array/89004/3 "2007-03-16T13:31:11Z")

</div>

RubyCrazy wrote:

> Is there a way to convert a String to an Array? I could only find the  
> following -  
> ‘hello world’.scan(/./)

And what’s the problem with that? Or do you want a different result?

Alternatively you can do

‘hello world’.split(’ ')

or

‘hello world’.split(/\s+/)

if you want to split on any number of whitespace chars.

If you want to split just on single spaces, your original solution is  
faster.

Cheers,  
Peter  
\_\_  
[http://www.rubyrailways.com](http://www.rubyrailways.com) :: Ruby and Web2.0 blog  
[http://scrubyt.org](http://scrubyt.org) :: Ruby web scraping framework  
[http://rubykitchensink.ca/](http://rubykitchensink.ca/) :: The indexed archive of all things Ruby
