From: akbarhome Date: 2007-05-07T18:20:03+09:00 Subject: Re: separate Chinese and English! with Ruby On May 7, 4:12 pm, akbarhome wrote: > On May 7, 2:39 pm, Nanyang Zhan wrote: > > > > > Don't get me wrong, because I just want to know how to separate English > > words from a string with ruby. > > There are strings (UTF-8 encoded) to record people's name, > > like: > > > 摩根·弗里曼 Morgan Freeman > > 布鲁斯·威利斯 Bruce Willis > > 李小明 Lee xiao ming > > these strings containing Chinese name(without space between characters), > > separated by a space, following an English name > > > or > > Frank Darabont > > Just an English name. > > > Would you give me an idea how to separate these Chinese characters(if > > any)? > > > -- > > Posted viahttp://www.ruby-forum.com/. > > a = File.open('a.txt') > a.each {|x| puts x.split(' ', 2) } > Output: > 摩根·弗里曼 > Morgan Freeman > 布鲁斯·威利斯 > Bruce Willis > 李小明 > Lee xiao ming Sorry. Fixed version: a.each {|x| if x[0].to_i > 128 then puts x.split(' ', 2) else puts x end } This code is quick and dirty.