Logo

Programming-Idioms

  • Obj-C
  • Perl
($t = $s) =~ s/[^\x00-\x7f]+//g;

Assign s to t and then operate on t, by removing the complement of the set of ASCII characters.
use utf8;
use Encode qw( encode decode );
$t = decode('ascii', encode('ascii', $s, sub { return '' } ) );

Using the Encode CPAN module to encode with a custom handler for conversion errors that returns an empty string. Same approach as the python example.
function Only_ASCII (S : String) return String is
   subtype ASCII is Character range
      Character'Val (0) .. Character'Val (127);
   T    : String (S'Range);
   Last : Natural := T'First - 1;
begin
   for Char of S loop
      if Char in ASCII then
         Last := Last + 1;
         T (Last) := Char;
      end if;
   end loop;
   return T (T'First .. Last);
end Only_ASCII;

New implementation...
< >
programming-idioms.org