Strip control codes and extended characters from a string: Difference between revisions

(→‎{{header|Perl 6}}: Fix syntax errors)
Line 1,302:
kşaNĹĭŗ|Ęw"ÄlĄWł8iCƁ꯬5ĎĶ'óü¸'ÍŸ;ŢƐ¦´ŷQċűÒŴ$ÃŅĐįð+=ĥƂ+Ōĭħ¼ŕc¤H~ìïēÕ
kaN|w"lW8iC5'';Q$+=+cH~</pre>
 
=={{header|Phix}}==
{{trans|Ada}}
While you can delete a character from a string using say s[i..i] = "", the fastest and easiest way is always just
to build a new one character-by-character.<br>
I've credited Ada solely for the sensible fromch/toch/abovech idea.
<lang Phix>function filter(string s, integer fromch=' ', toch=#7E, abovech=#7F)
string res = ""
for i=1 to length(s) do
integer ch = s[i]
if ch>=fromch and (ch<=toch or ch>abovech) then
res &= ch
end if
end for
return res
end function
procedure put_line(string text, s)
printf(1,"%s \"%s\", Length:%d\n",{text,s,length(s)})
end procedure
string full = "\u0000 abc\u00E9def\u007F"
 
put_line("The full string:", full)
put_line("No Control Chars:", filter(full)) -- default values for fromch, toch, and abovech
put_line("\" and no Extended:", filter(full, abovech:=#FF)) -- defaults for fromch and toch</lang>
{{out}}
<pre>
The full string: " abc+®def", Length:11
No Control Chars: " abc+®def", Length:9
" and no Extended: " abcdef", Length:7
</pre>
 
=={{header|PicoLisp}}==
7,806

edits