Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #147 Remove all non-ASCII characters

Create string t from string s, keeping only ASCII characters

 copy_if(begin(src), end(src), back_inserter(dest),
         [](const auto c) { return static_cast<unsigned char>(c) <= 0x7F; });
string t = Regex.Replace(s, @"[^\u0000-\u007F]+", string.Empty);
import std.array;
import std.algorithm.iteration;
import std.ascii;
auto t = s.filter!(a => a.isASCII).array;
t = 
  s 
  |> String.to_charlist()
  |> Enum.filter(&(&1 in 0..127))
  |> List.to_string
t = for <<c <- s>>, c in 0..127, into: "", do: <<c>>
  n = 0
  do i=1,len(s)
     if (iachar(s(i:i)) <= 127) n = n + 1
  end do
  allocate (character(len=n) :: t)
  j = 0
  do i=1,len(s)
     if (iachar(s(i:i)) <= 127) then
        j = j + 1
        t(j:j) = s(i:i)
     end if
  end do
import "regexp"
re := regexp.MustCompile("[[:^ascii:]]")
t := re.ReplaceAllLiteralString(s, "")
import (
	"fmt"
	"strings"
	"unicode"
)
t := strings.Map(func(r rune) rune {
	if r > unicode.MaxASCII {
		return -1
	}
	return r
}, s)
import Data.Char
f = filter isAscii
t = f s
import Data.Char
t = filter isAscii s
String t = s.replaceAll("[^\\x00-\\x7F]", "");
(remove-if-not (lambda (i) (<= 0 i 127))
               s
               :key #'char-code)
$t = preg_replace('/[^[:ascii:]]/', '', $s);
uses RegExpr;
var
  s,t: string;
begin
  t := ReplaceRegExpr('[^\u0000-\u007F]+', s, '', False);
end.
var
  i: integer;
begin
  t := '';
  for i := 1 to length(s) do
    if s[i] < #128 then t := t + s[i];
end.
($t = $s) =~ s/[^\x00-\x7f]+//g;
import re
t = re.sub('[^\u0000-\u007f]', '',  s)
t = s.gsub(/[^[:ascii:]]/, "")
t = s.gsub(/[[:^ascii:]]/, "")
let t = s.replace(|c: char| !c.is_ascii(), "");
let t = s.chars().filter(|c| c.is_ascii()).collect::<String>();

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms