简体   繁体   中英

Splitting a string into an array ruby

I am trying to turn this string

"a,bc,c"

into this array..

["a", "b", "c"]

I've used split on the comma & iterated through it but I'd like to find a cleaner way.

Thanks!

I will use #scan and #uniq method.

"a, bc,c".scan(/[a-z]/).uniq
# => ["a", "b", "c"]

Here we go, one option:

"a, bc,c".gsub(/\W+/, '').chars.uniq

 # Outputs:
 => ["a", "b", "c"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM