[MacRuby-devel] Scanning Unicode strings for non-ascii characters

Manfred Stienstra manfred at gmail.com
Tue Mar 3 03:45:49 PST 2009


On Mar 3, 2009, at 12:37 PM, Robert Schaaf wrote:

> This may be obvious, but in a Unicode world it's driving me nuts.   
> Given an arbitrary string, which may contain unicode characters, how  
> do I replace all characters not in the range 0x20..0x7e with spaces?


This isn't really a MacRuby related question, but here you go (:

   string.unpack('U*').select { |c| (0x20..0x7e).include? 
(c) }.pack('U*')

There are probably 200 other solutions, but this seems to be the  
easiest one. Remember that this not very fast and you probably want to  
use Iconv or something if your processing large pieces of text.

Manfred


More information about the MacRuby-devel mailing list