-1

I want to split a string of text into single chars excluding any kind of symbol and numbers and only allowing space with rails using split but I cannot reproduce the desired result.

Like

only_words_and_spaces = /[^a-zA-Z\s]/

"Hello friends".split(only_words_and_spaces)

=> ["H", "e", "l", "l", "o", " ", "f", "r", "i", "e", "n", "d", "s"]

The regex seems to work well, but I cannot find a way to split that string into a single char array.

4

5 回答 5

1

你可以试试

 "Hello friends".split('')
于 2013-09-13T10:16:51.953 回答
1

有像

"你好朋友".scan(/./) 或 word = "你好朋友" word.each_byte{|b| 放 b.chr}

于 2013-09-13T10:17:36.160 回答
0

The following does the trick:

"Hello 32☃ friends".scan(/[a-zA-Z\s]/)
#=> ["H", "e", "l", "l", "o", " ", " ", "f", "r", "i", "e", "n", "d", "s"]

The original string contains numbers and the UTF-8 snowman, but they are not present in the character array.

于 2013-09-13T10:19:13.823 回答
0

您可以将字符串拆分为数组,如下所示:

"Hello friends".split(//)
于 2013-09-13T10:15:01.917 回答
0
only_words_and_spaces = /[^a-zA-Z\s]/
"Hello friends+-!".gsub(only_words_and_spaces, '').split('')
=> ["H", "e", "l", "l", "o", " ", "f", "r", "i", "e", "n", "d", "s"]
于 2013-09-13T10:17:37.650 回答