Logo

Programming-Idioms

Create string t from string s, keeping only ASCII characters
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
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;
(def t (clojure.string/replace s #"[^\x00-\x7F]" ""))
 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;
var t = '';
for(var c in s.runes) {
  if(c < 128){
    t = t + String.fromCharCode(c);
  }
}
t = for <<c <- s>>, c in 0..127, into: "", do: <<c>>
t = 
  s 
  |> String.to_charlist()
  |> Enum.filter(&(&1 in 0..127))
  |> List.to_string
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 (
	"fmt"
	"strings"
	"unicode"
)
t := strings.Map(func(r rune) rune {
	if r > unicode.MaxASCII {
		return -1
	}
	return r
}, s)
import "regexp"
re := regexp.MustCompile("[[:^ascii:]]")
t := re.ReplaceAllLiteralString(s, "")
import Data.Char
f = filter isAscii
t = f s
import Data.Char
t = filter isAscii s
const t = s.replace(/[^\x00-\x7f]/gu, '')
const t = [...s].filter(c => c.charCodeAt(0) <= 0x7f).join('')
import static java.lang.String.valueOf;
import static java.util.stream.Collectors.joining;
String t = s.chars()
    .filter(c -> c < 0x80)
    .mapToObj(x -> valueOf((char) x))
    .collect(joining());
String t = "";
char c;
int i, n = s.length();
for (i = 0; i < n; ++i)
    if ((c = s.charAt(i)) < 0x80)
        t = t + c;
String t = s.replaceAll("[^\\x00-\\x7F]", "");
(remove-if-not (lambda (i) (<= 0 i 127))
               s
               :key #'char-code)
$t = preg_replace('/[^[:ascii:]]/', '', $s);
var
  i: integer;
begin
  t := '';
  for i := 1 to length(s) do
    if s[i] < #128 then t := t + s[i];
end.
uses RegExpr;
var
  s,t: string;
begin
  t := ReplaceRegExpr('[^\u0000-\u007F]+', s, '', False);
end.
($t = $s) =~ s/[^\x00-\x7f]+//g;
use utf8;
use Encode qw( encode decode );
$t = decode('ascii', encode('ascii', $s, sub { return '' } ) );
import re
t = re.sub('[^\u0000-\u007f]', '',  s)
t = s.encode("ascii", "ignore").decode()
f = lambda x: ord(x) < 0x80
t = ''.join(filter(f, s))
from re import sub
t = sub(r'[^\x00-\x7f]', '', s)
from re import sub
t = sub(r'[^\0-\176]', '', s)
t = s.gsub(/[[:^ascii:]]/, "")
t = s.gsub(/[^[:ascii:]]/, "")
let t = s.chars().filter(|c| c.is_ascii()).collect::<String>();
let t = s.replace(|c: char| !c.is_ascii(), "");
t := s select: [:character | character isAscii].