Logo

Programming-Idioms

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

Idiom #274 Remove all white space characters

Create the string t from the string s, removing all the spaces, newlines, tabulations, etc.

using System;
using System.Linq;
var t = new string(s.Where(c => !Char.IsWhiteSpace(c)).ToArray());
[Ch || Ch <- "This is a\tstring with\nwhite\rspaces", Ch /= 8, Ch /= 9, Ch /= 10, Ch /= 13, Ch /= 32 ].
import "strings"
import "unicode"
t := strings.Map(func(r rune) rune {
	if unicode.IsSpace(r) {
		return -1
	}
	return r
}, s)
import Data.Char (isSpace)
t = filter (not . isSpace) s
let t = s.replace(/\s/g,'');
String t = s.replaceAll("\\s+", "");
import static java.lang.Character.isWhitespace;
String t = "";
for (char c : s.toCharArray())
    if (!isWhitespace(c)) t = t + c;
String t = "";
for (char c : s.toCharArray())
    switch (c) {
        case ' ', '\t', '\n', '\r' -> {}
        default -> t = t + c;
    }
import static java.lang.Character.isWhitespace;
import static java.lang.String.valueOf;
import static java.util.stream.Collectors.joining;
String t = s.chars()
    .filter(c -> !isWhitespace(c))
    .mapToObj(c -> valueOf((char) c))
    .collect(joining());
  SetLength(t, Length(s));
  i := 0;
  for Ch in S do
    if (Ch > #32) then
    begin
      Inc(i);
      t[i] := Ch;
    end;
  SetLength(t, i);
uses RegExpr;
t := ReplaceRegExpr('\s', s, '');
my $t = $s;
$t =~ s/\s*//g;
import re
t = re.sub('\\s', '', s)
t = ''.join(s.split())
from string import whitespace
f = lambda x: x not in whitespace
t = ''.join(filter(f, s))
t = s.gsub(/\s/, "")
t = s.gsub(/[[:space:]]/, "")
let t: String = s.chars().filter(|c| !c.is_whitespace()).collect();

New implementation...