From: Jim Menard Date: 2001-12-16T23:15:25+09:00 Subject: [ruby-talk:28677] Re: How do you do "character filtering" of a string using each_byte. olczyk@interaccess.com (Thaddeus L. Olczyk) writes: > I'm trying to do several things where I produce new strings from old > strings by manipulating characters, and am stuck. > As an example (please keep in mind this is an example and I am > interested in the general technique ), consider a method which takes > a string and produces a new string such that: > 1) all digits are removed. > 2) all lowercase are changed to uppercase. > 3) multiple spaces are converted to one space > 4) tab is converted to one space. I'd use regulare expressions and String methods instead. For example, str = "abc 123 DeF\tghi" str.gsub!(/[0-9]/, '') # Get rid of digits str.upcase!() # Lower case to upper case str.gsub!(/[ \t]+/, ' ') # Multiple spaces and tabs => ' ' p str # => "ABC DEF GHI" The last line may not be exactly what you want. To implement (3) and (4) exactly, you may want to do this instead str.gsub!(/ +/, ' ') # There are two spaces before the '+' str.gsub!(/[\t]/, ' ') # Tabs become single spaces [snip] > string.each_byte{ |c| # aside: is it legal to pt the brace on the > # next line? [snip] The brace, no. The |c|, yes. Jim -- Jim Menard, jimm@io.com, http://www.io.com/~jimm/ "Dvorak users of the world flgkd!" -- Kirsten Chevalier in rec.humor.oracle.d