Logo

Programming-Idioms

New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating resource.

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

Other implementations
import "regexp"
whitespaces := regexp.MustCompile(`\s+`)
t := whitespaces.ReplaceAllString(s, " ")
def t = s.replaceAll(/\s+/, ' ')
let t = s.replace(/\s+/g, ' ');
$t = preg_replace('/\s+/', ' ', $s);
$t = $s;
do $t = str_replace('  ', ' ', $t, $count); while($count);
var
  i, j: integer;
  t,s: string;
const
  whitespace = [#32,#13,#10,#9];
begin
  ....
  t := '';
  j := 0;
  setlength(t, length(s));
  for i := 1 to length(s) do
    if not ((s[i] in whitespace) and 
            ((i < length(s)) and (s[i+1] in whitespace))) then
    begin
      inc(j);
      t[j] := s[i];
    end;
  setlength(t,j);
end.
regexpr
  t := ReplaceRegExpr('\s+',s,' ',False);
sysutils
  t := s;
  while Pos('  ',t) > 0 do
    t := StringReplace(t, '  ', ' ', [rfReplaceAll]);
my $t = $s;
$t =~ s/ +/ /g;
my $t = $s;
$t =~ s/\s+/ /g;
import re
t = re.sub(' +', ' ', s)
t = s.squeeze(" ")
use regex::Regex;
let re = Regex::new(r"\s+").unwrap();
let t = re.replace_all(s, " ");