Harry Kakueki wrote:
Try something like this.
t = str.split(//).partition {|x| x=~/[a-z]|[A-Z]/ }
p t[0].join
p t[1].join
Harry
Thanks, KaKuEKi, but:
!!!!below code were tested under Ruby on Rails console!!!
str1 = "中文 English Words"
=> "中文 English Words"
str2 = "Ôkami: chi"
=> "Ôkami: chi"
t = str2.split(//).partition { |x| x=~/[a-z]|[A-Z]/}
=> [["k", "a", "m", "i", "c", "h", "i"], ["Ô", ":", " "]]
p t[0].join
"kamichi" ##########I want all non Chinese characters remained.
=> nil
t = str1.split(//).partition { |x| x=~/[a-z]|[A-Z]/}
=> [["E", "n", "g", "l", "i", "s", "h", "W", "o", "r", "d", "s"], ["中",
"文", " ", " "]]
p t[0].join
"EnglishWords" #######no space
=> nil
Harry Kakueki wrote:
Or this
str.split(//).partition {|x| x.length == 1 }
Harry
this time spaces are kept:
t = str1.split(//).partition {|x| x.length == 1 }
=> [[" ", "E", "n", "g", "l", "i", "s", "h", " ", "W", "o", "r", "d",
"s"], ["中", "文"]]
t[0].join
=> " English Words"
t = str2.split(//).partition {|x| x.length == 1 }
=> [["k", "a", "m", "i", ":", " ", "c", "h", "i"], ["Ô"]]
t[0].join
=> "kami: chi"
I think "Ô" may just like Chinese characters, so it is hard to take it
out.
···
On 5/7/07, Nanyang Zhan <sxain@hotmail.com> wrote:
--
Posted via http://www.ruby-forum.com/\.