Logo

Programming-Idioms

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

Idiom #221 Remove all non-digits characters

Create string t from string s, keeping only digit characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

my $t = $s;
$t =~ s/\D+//g;
($t = $s) =~ tr/0-9//cd;
(require '[clojure.string :as str])
(let [s "1a22b3c4de5f6"
      t (str/replace s #"[^\d]" "")]
  (println t))
using System.Linq;
var t = string.Concat(s.Where(c => char.IsDigit(c)));
var t = String.fromCharCodes(
	s.codeUnits.where((x) => (x ^ 0x30) <= 9) );
import "regexp"
re := regexp.MustCompile("[^\\d]")
t := re.ReplaceAllLiteralString(s, "")
t = filter (`elem` ['0'..'9']) s
t = s.replace(/[^\d]/gm,"");
let t = s.replaceAll(/\D/g, '')
String t = s.replaceAll("\\D+", "");
StringBuilder tb = new StringBuilder();
for (int i=0; i<s.length(); i++){
    if(Character.isDigit(s.charAt(i)))
        tb.append(s.charAt(i));
}
String t = tb.toString();
import static java.lang.Character.isDigit;
String t = "";
for (char c : s.toCharArray())
    if (isDigit(c)) t = t + c;
import static java.lang.Character.isDigit;
String t = "";
char c;
int i, n = s.length();
for (i = 0; i < n; ++i)
    if (isDigit(c = s.charAt(i)))
        t = t + c;
import static java.lang.String.valueOf;
import static java.util.stream.Collectors.joining;
String t = s.chars()
    .filter(Character::isDigit)
    .mapToObj(x -> valueOf((char) x))
    .collect(joining());
(let ((*t* (remove-if-not #'digit-char-p *s*)))
	(format t "~A~%" *t*))
for i := 1 to length(s) do
    if s[i] in ['0'..'9'] then
        t := t + s[i];
uses RegExpr;
t := ReplaceRegExpr('[^\d]', s, ''); 
import re
t = re.sub(r"\D", "", s)
from string import digits
t = ''.join(x for x in s if x in digits)
t = s.delete("^0-9")
let t: String = s.chars().filter(|c| c.is_digit(10)).collect();

New implementation...