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

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 6 users not shown)
Line 809:
abcédef
abcdef</pre>
 
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
{String pack with control and extened chars}
 
const TestStr ='N'+#$01 +'o'+#$02 +'w'+#$03 +' '+#$04 +'i'+#$05 +'s'+#$06 +' '+#$07 +'t'+#$08 +'h'+#$09 +'e'+#$0A +' '+#$0B +'t'+#$0C +'i'+#$0D +'m'+#$0E +'e'+#$0F +' '+#$10 +'f'+#$11 +'o'+#$12 +'r'+#$13 +' '+#$14 +'a'+#$15 +'l'+#$16 +'l'+#$17 +' '+#$18 +'g'+#$19 +'o'+#$1A +'o'+#$1B +'d'+#$1C +' '+#$1D +'m'+#$1E +'e'+#$1F +'n'+#$80 +' '+#$81 +'t'+#$82 +'o'+#$83 +' '+#$84 +'c'+#$85 +'o'+#$86 +'m'+#$87 +'e'+#$88 +' '+#$89 +'t'+#$8A +'o'+#$8B +' '+#$8C +'t'+#$8D +'h'+#$8E +'e'+#$8F +' '+#$90 +'a'+#$91 +'i'+#$92 +'d'+#$93 +' '+#$94 +'o'+#$95 +'f'+#$96 +' '+#$97 +'t'+#$98 +'h'+#$99 +'e'+#$9A +' '+#$9B +'p'+#$9C +'a'+#$9D +'r'+#$9E +'t'+#$9F +'y'+#$A0;
 
function StripControls(S: string): string;
{Strip control characters from string}
var I: integer;
begin
Result:='';
for I:=1 to Length(S) do
if byte(S[I])>=$20 then Result:=Result+S[I];
end;
 
function StripExtended(S: string): string;
{Strip extended characters from string}
var I: integer;
begin
Result:='';
for I:=1 to Length(S) do
if byte(S[I])<$80 then Result:=Result+S[I];
end;
 
 
procedure StripString(Memo: TMemo);
begin
Memo.Lines.Add('String full of controls and extended chars: ');
Memo.Lines.Add(TestStr);
Memo.Lines.Add('String stripped of controls chars: ');
Memo.Lines.Add(StripControls(TestStr));
Memo.Lines.Add('String stripped of extended chars: ');
Memo.Lines.Add(StripExtended(TestStr));
Memo.Lines.Add('String stripped of both control and extended chars: ');
Memo.Lines.Add(StripControls(StripExtended(TestStr)));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
String full of controls and extended chars:
N�o�w� �i�s� �t�h e
�t�i
m�e� �f�o�r� �a�l�l� �g�o�o�d� �m�e�n€ t‚oƒ „c…o†m‡eˆ ‰tŠo‹ ŒthŽe a‘i’d“ ”o•f– —t˜h™eš ›pœaržtŸy 
String stripped of controls chars:
Now is the time for all good men€ t‚oƒ „c…o†m‡eˆ ‰tŠo‹ ŒthŽe a‘i’d“ ”o•f– —t˜h™eš ›pœaržtŸy 
String stripped of extended chars:
N�o�w� �i�s� �t�h e
�t�i
m�e� �f�o�r� �a�l�l� �g�o�o�d� �m�e�n to come to the aid of the party
String stripped of both control and extended chars:
Now is the time for all good men to come to the aid of the party
Elapsed Time: 51.012 ms.
 
</pre>
 
 
=={{header|EasyLang}}==
 
<syntaxhighlight lang=easylang>
func$ strip s$ .
for c$ in strchars s$
if strcode c$ >= 32 and strcode c$ <= 126
r$ &= c$
.
.
return r$
.
print strip "\tHellö world"
</syntaxhighlight>
 
=={{header|Erlang}}==
Line 1,414 ⟶ 1,492:
 
writeln "original : ", .str
writeln "without ctrl chars: ", replace(.str, RE/\p{Cc}/, ZLS"")
writeln "print ASCII only : ", replace(.str, re/[^ -~]/, ZLS"")</syntaxhighlight>
 
{{out}}
Line 1,547 ⟶ 1,625:
 
<syntaxhighlight lang="ocaml">let is_control_code c =
letc d< ='\032' int_of_char|| c in= '\127'
 
d < 32 || d = 127
let is_extended_char c =
c > '\127'
let d = int_of_char c in
 
d > 127
let strip f str =
let len = String.length str in
Line 1,559 ⟶ 1,635:
let rec aux i j =
if i >= len
then Bytes.to_string (Bytes.subsub_string res 0 j)
else if f str.[i]
then aux (succ i) j
Line 1,568 ⟶ 1,644:
in
aux 0 0
 
let () =
Random.self_init ();
Line 1,577 ⟶ 1,653:
in
print_endline (strip is_control_code s);
print_endline (strip (fun c -> (is_control_code c) || (is_extended_char c)) s);</syntaxhighlight>
;;</syntaxhighlight>
 
=={{header|Pascal}}==
Line 2,017 ⟶ 2,092:
return strip
</syntaxhighlight>
 
=={{header|RPL}}==
RPL has a character set based on ASCII but does not support extended characters.
≪ → text
≪ "" 1 text SIZE '''FOR''' j
text j DUP SUB NUM
'''IF''' DUP 32 ≥ OVER 126 ≤ '''THEN''' CHR + '''ELSE''' DROP '''END'''
'''NEXT'''
≫ ≫ '<span style="color:blue">NOCTRL</span>' STO
 
=={{header|Ruby}}==
Line 2,148 ⟶ 2,232:
&#32;&#68;&#113;&#49;&#91;&#58;&#51;&#83;&#80;&#77;&#110;&#67;&#90;&#60;&#74;&#43;&#102;&#117;&#34;&#93;&#109;&#92;&#50;&#54;&#124;&#106;&#85;&#64;&#101;&#96;&#78;&#63;&#95;&#39;&#75;&#126;&#112;&#115;&#46;&#98;&#105;&#72;&#55;&#62;&#122;&#65;&#88;&#86;&#70;&#53;
&#61;&#79;&#103;&#66;&#104;&#89;&#71;&#99;&#45;&#52;&#41;&#69;&#47;&#42;&#97;&#44;&#37;&#119;&#84;&#76;&#111;&#82;&#38;&#87;&#123;&#107;&#100;&#125;&#56;&#108;&#94;&#59;&#48;&#35;&#40;&#33;&#116;&#114;&#118;&#73;&#120;&#36;&#81;&#121;&#57;</pre>
 
=={{header|sed}}==
To strip control codes only:
<syntaxhighlight lang="sed">s/[[:cntrl:]]//g</syntaxhighlight>
To strip control codes and extended characters:
<syntaxhighlight lang="sed">s/[^[:print:]]//g</syntaxhighlight>
For this to work properly with sed implementations supporting multibyte character encodings (like UTF-8), the environment variable LC_ALL=C might need to be set.
{{out}}
<pre>
$ printf 'Just\tä\tString\n' | LC_ALL=C sed 's/[[:cntrl:]]//g'
JustäString
$ printf 'Just\tä\tString\n' | LC_ALL=C sed 's/[^[:print:]]//g'
JustString
</pre>
 
=={{header|Seed7}}==
Line 2,321 ⟶ 2,419:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|AutoHotkey}}
<syntaxhighlight lang="v (vlang)">fn main() {
println(stripped("\ba\x00b\n\rc\fd\xc3"))
}
Line 2,340 ⟶ 2,438:
=={{header|Wren}}==
{{libheader|Wren-pattern}}
<syntaxhighlight lang="ecmascriptwren">import "./pattern" for Pattern
 
var s = "\t\n\r\x01\0\fabc\v\v\b\a\x1f\x7f🌇Páez😃É"
9,482

edits