48

I have a variable with a string value. Say:

str = "hello, world"

How can I convert that into an array of the individual characters from the original string?

The desired result for the above example would be:

['h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
6
  • Would be helpful if you show the input and the output you desire. Commented Feb 2, 2011 at 2:44
  • yeah I know this but once i convert split string then i want to add another string into it and then want convert into array using join Commented Feb 2, 2011 at 2:48
  • 1
    "It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form." Commented Feb 2, 2011 at 2:55
  • Thanks, is there anything left that you want to say Commented Feb 2, 2011 at 3:20
  • 6
    is it that difficult to include examples of what you are trying to do? Commented Feb 2, 2011 at 4:30

7 Answers 7

98

I noticed that this page comes up for generic google search of "string to char array ruby".

If you just want to convert string to char array in ruby,

"string".chars
# ["s", "t", "r", "i", "n", "g"]
Sign up to request clarification or add additional context in comments.

1 Comment

short and sweet, just what i needed
29

i don't understand your question, but if you want to convert string to array

>> "a string".split("")
=> ["a", " ", "s", "t", "r", "i", "n", "g"]

3 Comments

yeah I know this but once i convert split string then i want to add another string into it and then want convert into array using join
you should provide example of what you want to do. I cannot visualize what you are saying.
"then want convert into array using join" This doesn't make sense. You can't convert a string into an array using join.
4
str = "a,b,c"
list = str.split(/,/) # => ["a", "b", "c"]
list.join("-") # => "a-b-c"

From your comment, it looks like you want to also append a string (or several strings) to the list and then join back into another string. Perhaps like this:

(str.split(/,/) << 'd').join(',') # => "a,b,c,d"
list2 = ['d', 'e', 'f']
(str.split(/,/) << list2).join(',') # => "a,b,c,d,e,f"

Ruby syntax also allows calling operators with the "dot" notation, so this might clarify the situation:

str.split(/,/).<<('d').join(',') # => "a,b,c,d"
str.split(/,/).<<(list2).join(',') # => "a,b,c,d,e,f"

1 Comment

str = "a,b,c" now we split str and result is ["a", "b","c"]. Now I want to add a string into str(after split) then want to join str +string using join(",") ?????
1
str = "a,b,c"
 => "a,b,c" 

array = str.split(/,/)
 => ["a", "b", "c"] 

# add more elements to the str:

str << ',x,y,z'
 => "a,b,c,x,y,z" 

array2 =  str.split(/,/)
 => ["a", "b", "c", "x", "y", "z"] 

but you probably want something like this (process input strings and assemble them in an array):

array = []
str = "a,b,c"
array << str.split(/,/)
  => [["a", "b", "c"]] 

str = 'x,y,z'
array << str.split(/,/)
 => [["a", "b", "c"], ["x", "y", "z"]] 

array.flatten!
 => ["a", "b", "c", "x", "y", "z"] 

Comments

0

If you are trying to have your "list" be a string representation of a collection of names, and you want to add a name to that (also a string) delimited from the rest by (for example) a comma, and have the result be a string, all you need is string concatenation:

list + ',' + name

A nicer way to do this, at least to me, is string interpolation:

"#{list},#{name}"

This makes it clearer that you are manipulating data to format it.

However, if you want to have your "list" converted to an array by splitting it by commas and add the name variable to the end, the answer is essentially the same, with String#split and array concatenation:

list.split(',') + name

Hopefully one of these two is the solution to your problem!

Comments

0

I think the big problem is you're trying to use a string, when you should really be using an array. A second problem is that you're confusing a comma-delimited string with an array or list.

In Ruby, lists and arrays are different objects than strings. Strings are good for storing text and sometimes binary data.

Arrays are good for storing sequences of data, where you need to be able to access them by a numerical index. Queues and stacks are generally written on top of arrays, because arrays make it easy to add and remove elements from the start or end.

For your purpose I'd recommend using an Array object, then join(',') the array when you need the string representation.

ary = []        #=> []
ary << 'item 1' #=> ["item 1"]
ary << 'item 2' #=> ["item 1", "item 2"]
ary.join(',')   #=> "item 1,item 2"

Comments

0

don't forget scan

in my case I have a true friend code, a "big" integer (8 digits), for better readability I want to put dashes every 2 digits:

friend_code.scan(/../).join("-")

your solution would be

friend_code.scan(/./).join(",") 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.