Strip control codes and extended characters from a string: Difference between revisions
Content deleted Content added
add BQN |
Thundergnat (talk | contribs) m syntax highlighting fixup automation |
||
Line 22: | Line 22: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F stripped(s) |
||
R s.filter(i -> Int(i.code) C 32..126).join(‘’) |
R s.filter(i -> Int(i.code) C 32..126).join(‘’) |
||
print(stripped("\ba\u0000b\n\rc\fd\xc3"))</ |
print(stripped("\ba\u0000b\n\rc\fd\xc3"))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 33: | Line 33: | ||
=={{header|8086 Assembly}}== |
=={{header|8086 Assembly}}== |
||
< |
<syntaxhighlight lang="asm"> .model small |
||
.stack 1024 |
.stack 1024 |
||
.data |
.data |
||
Line 104: | Line 104: | ||
int 10h |
int 10h |
||
ret |
ret |
||
end start</ |
end start</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 113: | Line 113: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">BYTE FUNC IsAscii(CHAR c) |
||
IF c<32 OR c>124 OR c=96 OR c=123 THEN |
IF c<32 OR c>124 OR c=96 OR c=123 THEN |
||
RETURN (0) |
RETURN (0) |
||
Line 142: | Line 142: | ||
Strip(src,dst) |
Strip(src,dst) |
||
PrintF("Stripped string: ""%S""%E",dst) |
PrintF("Stripped string: ""%S""%E",dst) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strip_control_codes_and_extended_characters_from_a_string.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strip_control_codes_and_extended_characters_from_a_string.png Screenshot from Atari 8-bit computer] |
||
Line 152: | Line 152: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Strip_ASCII is |
procedure Strip_ASCII is |
||
Line 187: | Line 187: | ||
Put_Line("Neither_Extended:", Filter(Full, Above => Character'Last)); -- defaults for From and To |
Put_Line("Neither_Extended:", Filter(Full, Above => Character'Last)); -- defaults for From and To |
||
end Strip_ASCII; |
end Strip_ASCII; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
Line 197: | Line 197: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68"># remove control characters and optionally extended characters from the string text # |
||
# assums ASCII is the character set # |
# assums ASCII is the character set # |
||
PROC strip characters = ( STRING text, BOOL strip extended )STRING: |
PROC strip characters = ( STRING text, BOOL strip extended )STRING: |
||
Line 226: | Line 226: | ||
STRING t = REPR 2 + "abc" + REPR 10 + REPR 160 + "def~" + REPR 127 + REPR 10 + REPR 150 + REPR 152 + "!"; |
STRING t = REPR 2 + "abc" + REPR 10 + REPR 160 + "def~" + REPR 127 + REPR 10 + REPR 150 + REPR 152 + "!"; |
||
print( ( "<<" + t + ">> - without control characters: <<" + strip characters( t, FALSE ) + ">>", newline ) ); |
print( ( "<<" + t + ">> - without control characters: <<" + strip characters( t, FALSE ) + ">>", newline ) ); |
||
print( ( "<<" + t + ">> - without control or extended characters: <<" + strip characters( t, TRUE ) + ">>", newline ) )</ |
print( ( "<<" + t + ">> - without control or extended characters: <<" + strip characters( t, TRUE ) + ">>", newline ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 238: | Line 238: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">str: {string of ☺☻♥♦⌂, may include control characters and other ♫☼§►↔◄░▒▓█┌┴┐±÷²¬└┬┘ilk.} |
||
print "with extended characters" |
print "with extended characters" |
||
Line 247: | Line 247: | ||
print join select split str 'x -> |
print join select split str 'x -> |
||
and? ascii? x |
and? ascii? x |
||
not? in? to :integer to :char x (0..31)++127</ |
not? in? to :integer to :char x (0..31)++127</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 258: | Line 258: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="ahk">Stripped(x){ |
||
Loop Parse, x |
Loop Parse, x |
||
if Asc(A_LoopField) > 31 and Asc(A_LoopField) < 128 |
if Asc(A_LoopField) > 31 and Asc(A_LoopField) < 128 |
||
Line 264: | Line 264: | ||
return r |
return r |
||
} |
} |
||
MsgBox % stripped("`ba" Chr(00) "b`n`rc`fd" Chr(0xc3))</ |
MsgBox % stripped("`ba" Chr(00) "b`n`rc`fd" Chr(0xc3))</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f STRIP_CONTROL_CODES_AND_EXTENDED_CHARACTERS.AWK |
# syntax: GAWK -f STRIP_CONTROL_CODES_AND_EXTENDED_CHARACTERS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 276: | Line 276: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<p>output:</p> |
<p>output:</p> |
||
<pre> |
<pre> |
||
Line 289: | Line 289: | ||
While DOS does support ''some'' extended characters, they aren't entirely standardized, and shouldn't be relied upon. |
While DOS does support ''some'' extended characters, they aren't entirely standardized, and shouldn't be relied upon. |
||
< |
<syntaxhighlight lang="qbasic">DECLARE FUNCTION strip$ (what AS STRING) |
||
DECLARE FUNCTION strip2$ (what AS STRING) |
DECLARE FUNCTION strip2$ (what AS STRING) |
||
Line 326: | Line 326: | ||
NEXT |
NEXT |
||
strip2$ = outP |
strip2$ = outP |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
Output: |
Output: |
||
Line 336: | Line 336: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
< |
<syntaxhighlight lang="bbcbasic"> test$ = CHR$(9) + "Fran" + CHR$(231) + "ais." + CHR$(127) |
||
PRINT "Original ISO-8859-1 string: " test$ " (length " ; LEN(test$) ")" |
PRINT "Original ISO-8859-1 string: " test$ " (length " ; LEN(test$) ")" |
||
test$ = FNstripcontrol(test$) |
test$ = FNstripcontrol(test$) |
||
Line 362: | Line 362: | ||
ENDIF |
ENDIF |
||
ENDWHILE |
ENDWHILE |
||
= A$</ |
= A$</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 373: | Line 373: | ||
Using BQN's character arithmetic and comparison, characters are binned using <code>⍋</code> and removed if they are inside the range. |
Using BQN's character arithmetic and comparison, characters are binned using <code>⍋</code> and removed if they are inside the range. |
||
< |
<syntaxhighlight lang="bqn">StripCt←((1≠(@+0‿32)⊸⍋)∧(@+127)⊸≠)⊸/ |
||
StripCtEx←(1=(@+32‿127)⊸⍋)⊸/</ |
StripCtEx←(1=(@+32‿127)⊸⍋)⊸/</syntaxhighlight> |
||
< |
<syntaxhighlight lang="bqn"> RP←•rand.Deal∘≠⊸⊏ # Random Permutation |
||
(rand).Deal∘≠⊸⊏ |
(rand).Deal∘≠⊸⊏ |
||
ascii←RP @+↕256 |
ascii←RP @+↕256 |
||
Line 400: | Line 400: | ||
967 |
967 |
||
≠StripCtEx unicode |
≠StripCtEx unicode |
||
95</ |
95</syntaxhighlight> |
||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
< |
<syntaxhighlight lang="bracmat">( "string of ☺☻♥♦⌂, may include control |
||
characters and other ilk.\L\D§►↔◄ |
characters and other ilk.\L\D§►↔◄ |
||
Rødgrød med fløde" |
Rødgrød med fløde" |
||
Line 429: | Line 429: | ||
) |
) |
||
) |
) |
||
& );</ |
& );</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>Control characters stripped: |
<pre>Control characters stripped: |
||
Line 447: | Line 447: | ||
A true/false function checks if the character is in the valid range.<br> |
A true/false function checks if the character is in the valid range.<br> |
||
< |
<syntaxhighlight lang="c">#include <stdlib.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 573: | Line 573: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 585: | Line 585: | ||
===<tt>apply mask from a table</tt>=== |
===<tt>apply mask from a table</tt>=== |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 648: | Line 648: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight>output:<syntaxhighlight lang="text"> !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ <odd stuff my xterm thinks are bad unicode hence can't be properly shown> |
||
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ |
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~ |
||
!"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~</ |
!"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
Uses the test string from REXX. |
Uses the test string from REXX. |
||
< |
<syntaxhighlight lang="csharp"> |
||
using System; |
using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 697: | Line 697: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 706: | Line 706: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp">#include <string> |
||
#include <iostream> |
#include <iostream> |
||
#include <algorithm> |
#include <algorithm> |
||
Line 748: | Line 748: | ||
std::cout << "string without extended characters: " << no_extended << std::endl ; |
std::cout << "string without extended characters: " << no_extended << std::endl ; |
||
return 0 ; |
return 0 ; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<PRE>string with all characters: K�O:~���7�5���� |
<PRE>string with all characters: K�O:~���7�5���� |
||
Line 758: | Line 758: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">; generate our test string of characters with control and extended characters |
||
(def range-of-chars (apply str (map char (range 256)))) |
(def range-of-chars (apply str (map char (range 256)))) |
||
Line 765: | Line 765: | ||
; filter to return String of characters that are between 32 - 126: |
; filter to return String of characters that are between 32 - 126: |
||
(apply str (filter #(<= 32 (int %) 126) range-of-chars))</ |
(apply str (filter #(<= 32 (int %) 126) range-of-chars))</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 786: | Line 786: | ||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">import std.traits; |
||
S stripChars(S)(S s, bool function(dchar) pure nothrow mustStrip) |
S stripChars(S)(S s, bool function(dchar) pure nothrow mustStrip) |
||
Line 804: | Line 804: | ||
writeln(s.stripChars( c => isControl(c) || c == '\u007F' )); |
writeln(s.stripChars( c => isControl(c) || c == '\u007F' )); |
||
writeln(s.stripChars( c => isControl(c) || c >= '\u007F' )); |
writeln(s.stripChars( c => isControl(c) || c >= '\u007F' )); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> abcédef� |
<pre> abcédef� |
||
Line 812: | Line 812: | ||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Exported functions to be used by [[Update_a_configuration_file]] |
Exported functions to be used by [[Update_a_configuration_file]] |
||
<syntaxhighlight lang="erlang"> |
|||
<lang Erlang> |
|||
-module( strip_control_codes ). |
-module( strip_control_codes ). |
||
Line 831: | Line 831: | ||
String_without_cc_nor_ec = lists:filter( fun is_not_control_code_nor_extended_character/1, String ), |
String_without_cc_nor_ec = lists:filter( fun is_not_control_code_nor_extended_character/1, String ), |
||
io:fwrite( "String without control codes nor extended characters (~p characters): ~s~n", [erlang:length(String_without_cc_nor_ec), String_without_cc_nor_ec] ). |
io:fwrite( "String without control codes nor extended characters (~p characters): ~s~n", [erlang:length(String_without_cc_nor_ec), String_without_cc_nor_ec] ). |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 843: | Line 843: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
Uses test string from REXX. |
Uses test string from REXX. |
||
< |
<syntaxhighlight lang="fsharp"> |
||
open System |
open System |
||
Line 862: | Line 862: | ||
printfn "Stripped of extended: %s" (stripExtended test) |
printfn "Stripped of extended: %s" (stripExtended test) |
||
0//main must return integer, much like in C/C++ |
0//main must return integer, much like in C/C++ |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 871: | Line 871: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang>USING: ascii kernel sequences ; |
<syntaxhighlight lang="text">USING: ascii kernel sequences ; |
||
: strip-control-codes ( str -- str' ) [ control? not ] filter ; |
: strip-control-codes ( str -- str' ) [ control? not ] filter ; |
||
: strip-control-codes-and-extended ( str -- str' ) |
: strip-control-codes-and-extended ( str -- str' ) |
||
strip-control-codes [ ascii? ] filter ;</ |
strip-control-codes [ ascii? ] filter ;</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
< |
<syntaxhighlight lang="forth">: strip ( buf len -- buf len' ) \ repacks buffer, so len' <= len |
||
over + over swap over ( buf dst limit src ) |
over + over swap over ( buf dst limit src ) |
||
do |
do |
||
Line 886: | Line 886: | ||
then |
then |
||
loop |
loop |
||
over - ;</ |
over - ;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
< |
<syntaxhighlight lang="fortran">module stripcharacters |
||
implicit none |
implicit none |
||
Line 943: | Line 943: | ||
write (*,*) strip(string,not_extended) |
write (*,*) strip(string,not_extended) |
||
end program test |
end program test |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Function stripControlChars(s As Const String) As String |
Function stripControlChars(s As Const String) As String |
||
Line 1,023: | Line 1,023: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,039: | Line 1,039: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
< |
<syntaxhighlight lang="frink">stripExtended[str] := str =~ %s/[^\u0020-\u007e]//g |
||
stripControl[str] := str =~ %s/[\u0000-\u001F\u007f]//g |
stripControl[str] := str =~ %s/[\u0000-\u001F\u007f]//g |
||
println[stripExtended[char[0 to 127]]] |
println[stripExtended[char[0 to 127]]] |
||
println[stripControl[char[0 to 127]]]</ |
println[stripControl[char[0 to 127]]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,053: | Line 1,053: | ||
=={{header|Gambas}}== |
=={{header|Gambas}}== |
||
'''[https://gambas-playground.proko.eu/?gist=19db658a6c44cfb1f6f887ff53e549bb Click this link to run this code]''' |
'''[https://gambas-playground.proko.eu/?gist=19db658a6c44cfb1f6f887ff53e549bb Click this link to run this code]''' |
||
< |
<syntaxhighlight lang="gambas">Public Sub Main() |
||
Dim sString As String = "The\t \equick\n \fbrownfox \vcost £125.00 or €145.00 or $160.00 \bto \ncapture ©®" |
Dim sString As String = "The\t \equick\n \fbrownfox \vcost £125.00 or €145.00 or $160.00 \bto \ncapture ©®" |
||
Dim sStd, sExtend As String |
Dim sStd, sExtend As String |
||
Line 1,083: | Line 1,083: | ||
Return sResult |
Return sResult |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,097: | Line 1,097: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Go works for ASCII and non-ASCII systems. The first pair of functions below interpret strings as byte strings, presumably useful for strings consisting of ASCII and 8-bit extended ASCII data. The second pair of functions interpret strings as UTF-8. |
Go works for ASCII and non-ASCII systems. The first pair of functions below interpret strings as byte strings, presumably useful for strings consisting of ASCII and 8-bit extended ASCII data. The second pair of functions interpret strings as UTF-8. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,186: | Line 1,186: | ||
fmt.Println("\nas decomposed and stripped Unicode:") |
fmt.Println("\nas decomposed and stripped Unicode:") |
||
fmt.Println(stripCtlAndExtFromUnicode(src)) |
fmt.Println(stripCtlAndExtFromUnicode(src)) |
||
}</ |
}</syntaxhighlight> |
||
Output: (varies with display configuration) |
Output: (varies with display configuration) |
||
<pre> |
<pre> |
||
Line 1,211: | Line 1,211: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy">def stripControl = { it.replaceAll(/\p{Cntrl}/, '') } |
||
def stripControlAndExtended = { it.replaceAll(/[^\p{Print}]/, '') }</ |
def stripControlAndExtended = { it.replaceAll(/[^\p{Print}]/, '') }</syntaxhighlight> |
||
Test: |
Test: |
||
< |
<syntaxhighlight lang="groovy">def text = (0..255).collect { (char) it }.join('') |
||
def textMinusControl = text.findAll { int v = (char)it; v > 31 && v != 127 }.join('') |
def textMinusControl = text.findAll { int v = (char)it; v > 31 && v != 127 }.join('') |
||
def textMinusControlAndExtended = textMinusControl.findAll {((char)it) < 128 }.join('') |
def textMinusControlAndExtended = textMinusControl.findAll {((char)it) < 128 }.join('') |
||
assert stripControl(text) == textMinusControl |
assert stripControl(text) == textMinusControl |
||
assert stripControlAndExtended(text) == textMinusControlAndExtended</ |
assert stripControlAndExtended(text) == textMinusControlAndExtended</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Control.Applicative (liftA2) |
||
strip, strip2 :: String -> String |
strip, strip2 :: String -> String |
||
Line 1,233: | Line 1,233: | ||
main = |
main = |
||
(putStrLn . unlines) $ |
(putStrLn . unlines) $ |
||
[strip, strip2] <*> ["alphabetic 字母 with some less parochial parts"]</ |
[strip, strip2] <*> ["alphabetic 字母 with some less parochial parts"]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>alphabetic with some less parochial parts |
<pre>alphabetic with some less parochial parts |
||
Line 1,240: | Line 1,240: | ||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
We'll use ''deletec'' to remove unwanted characters (2nd argument) from a string (1st argument). The procedure below coerces types back and forth between string and cset. The character set of unwanted characters is the difference of all ASCII characters and the ASCII characters from 33 to 126. |
We'll use ''deletec'' to remove unwanted characters (2nd argument) from a string (1st argument). The procedure below coerces types back and forth between string and cset. The character set of unwanted characters is the difference of all ASCII characters and the ASCII characters from 33 to 126. |
||
< |
<syntaxhighlight lang="icon">procedure main(A) |
||
write(image(deletec(&ascii,&ascii--(&ascii)[33:127]))) |
write(image(deletec(&ascii,&ascii--(&ascii)[33:127]))) |
||
end |
end |
||
link strings |
link strings |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
Line 1,250: | Line 1,250: | ||
The IPL procedure ''deletec'' is equivalent to this: |
The IPL procedure ''deletec'' is equivalent to this: |
||
< |
<syntaxhighlight lang="icon">procedure deletec(s, c) #: delete characters |
||
result := "" |
result := "" |
||
s ? { |
s ? { |
||
Line 1,256: | Line 1,256: | ||
return result ||:= tab(0) |
return result ||:= tab(0) |
||
} |
} |
||
end</ |
end</syntaxhighlight> |
||
Line 1,263: | Line 1,263: | ||
=={{header|J}}== |
=={{header|J}}== |
||
'''Solution:''' |
'''Solution:''' |
||
< |
<syntaxhighlight lang="j">stripControlCodes=: -.&(DEL,32{.a.) |
||
stripControlExtCodes=: ([ -. -.)&(32}.127{.a.)</ |
stripControlExtCodes=: ([ -. -.)&(32}.127{.a.)</syntaxhighlight> |
||
'''Usage:''' |
'''Usage:''' |
||
< |
<syntaxhighlight lang="j"> mystring=: a. {~ ?~256 NB. ascii chars 0-255 in random order |
||
#mystring NB. length of string |
#mystring NB. length of string |
||
256 |
256 |
||
Line 1,280: | Line 1,280: | ||
95 |
95 |
||
stripControlExtCodes myunicodestring |
stripControlExtCodes myunicodestring |
||
k}w:]U3xEh9"GZdr/#^B.Sn%\uFOo[(`t2-J6*IA=Vf&N;lQ8,${XLz5?D0~s)'Y7Kq|ip4<WRCaM!b@cgv_T +mH>1ejPy</ |
k}w:]U3xEh9"GZdr/#^B.Sn%\uFOo[(`t2-J6*IA=Vf&N;lQ8,${XLz5?D0~s)'Y7Kq|ip4<WRCaM!b@cgv_T +mH>1ejPy</syntaxhighlight> |
||
Generally speaking, <code>([-.-.)</code> gives us the contents from the sequence on the left, restricted to only the items which appear in the sequence on the right. |
Generally speaking, <code>([-.-.)</code> gives us the contents from the sequence on the left, restricted to only the items which appear in the sequence on the right. |
||
Line 1,288: | Line 1,288: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|8+}} |
{{works with|Java|8+}} |
||
< |
<syntaxhighlight lang="java">import java.util.function.IntPredicate; |
||
public class StripControlCodes { |
public class StripControlCodes { |
||
Line 1,302: | Line 1,302: | ||
StringBuilder::appendCodePoint, StringBuilder::append).toString(); |
StringBuilder::appendCodePoint, StringBuilder::append).toString(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre> abcédef |
<pre> abcédef |
||
abcdef</pre> |
abcdef</pre> |
||
Line 1,310: | Line 1,310: | ||
===ES 5=== |
===ES 5=== |
||
< |
<syntaxhighlight lang="javascript">(function (strTest) { |
||
// s -> s |
// s -> s |
||
Line 1,323: | Line 1,323: | ||
return strip(strTest); |
return strip(strTest); |
||
})("\ba\x00b\n\rc\fd\xc3");</ |
})("\ba\x00b\n\rc\fd\xc3");</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight lang="javascript">"abcd"</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|1.4}} |
{{works with|jq|1.4}} |
||
< |
<syntaxhighlight lang="jq">def strip_control_codes: |
||
explode | map(select(. > 31 and . != 127)) | implode; |
explode | map(select(. > 31 and . != 127)) | implode; |
||
def strip_extended_characters: |
def strip_extended_characters: |
||
explode | map(select(31 < . and . < 127)) | implode;</ |
explode | map(select(31 < . and . < 127)) | implode;</syntaxhighlight> |
||
'''Example''': |
'''Example''': |
||
< |
<syntaxhighlight lang="jq">def string: "string of ☺☻♥♦⌂, may include control characters such as null(\u0000) and other ilk.\n§►↔◄\nRødgrød med fløde"; |
||
"string | strip_control_codes\n => \(string | strip_control_codes)", |
"string | strip_control_codes\n => \(string | strip_control_codes)", |
||
"string | strip_extended_characters\n => \(string | strip_extended_characters)"</ |
"string | strip_extended_characters\n => \(string | strip_extended_characters)"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">$ jq -n -r -f Strip_control_codes_and_extended_characters.jq |
||
string | strip_control_codes |
string | strip_control_codes |
||
=> string of ☺☻♥♦⌂, may include control characters such as null() and other ilk.§►↔◄Rødgrød med fløde |
=> string of ☺☻♥♦⌂, may include control characters such as null() and other ilk.§►↔◄Rødgrød med fløde |
||
string | strip_extended_characters |
string | strip_extended_characters |
||
=> string of , may include control characters such as null() and other ilk.Rdgrd med flde</ |
=> string of , may include control characters such as null() and other ilk.Rdgrd med flde</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
stripc0{T<:String}(a::T) = replace(a, r"[\x00-\x1f\x7f]", "") |
stripc0{T<:String}(a::T) = replace(a, r"[\x00-\x1f\x7f]", "") |
||
stripc0x{T<:String}(a::T) = replace(a, r"[^\x20-\x7e]", "") |
stripc0x{T<:String}(a::T) = replace(a, r"[^\x20-\x7e]", "") |
||
Line 1,359: | Line 1,359: | ||
println("\nWith C0 control characters removed:\n ", stripc0(a)) |
println("\nWith C0 control characters removed:\n ", stripc0(a)) |
||
println("\nWith C0 and extended characters removed:\n ", stripc0x(a)) |
println("\nWith C0 and extended characters removed:\n ", stripc0x(a)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,375: | Line 1,375: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
fun String.strip(extendedChars: Boolean = false): String { |
fun String.strip(extendedChars: Boolean = false): String { |
||
Line 1,396: | Line 1,396: | ||
val u = s.strip(true) |
val u = s.strip(true) |
||
println("String = $u Length = ${u.length}") |
println("String = $u Length = ${u.length}") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,411: | Line 1,411: | ||
=={{header|langur}}== |
=={{header|langur}}== |
||
< |
<syntaxhighlight lang="langur">val .str = "()\x15abcd\uFFFF123\uBBBB!@#$%^&*\x01" |
||
writeln "original : ", .str |
writeln "original : ", .str |
||
writeln "without ctrl chars: ", replace(.str, RE/\p{Cc}/, ZLS) |
writeln "without ctrl chars: ", replace(.str, RE/\p{Cc}/, ZLS) |
||
writeln "print ASCII only : ", replace(.str, re/[^ -~]/, ZLS)</ |
writeln "print ASCII only : ", replace(.str, re/[^ -~]/, ZLS)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,423: | Line 1,423: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
all$ ="" |
all$ ="" |
||
for i =0 to 255 |
for i =0 to 255 |
||
Line 1,461: | Line 1,461: | ||
extendedStripped$ =r$ |
extendedStripped$ =r$ |
||
end function |
end function |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">function Strip_Control_Codes( str ) |
||
local s = "" |
local s = "" |
||
for i in str:gmatch( "%C+" ) do |
for i in str:gmatch( "%C+" ) do |
||
Line 1,488: | Line 1,488: | ||
print( Strip_Control_Codes(q) ) |
print( Strip_Control_Codes(q) ) |
||
print( Strip_Control_and_Extended_Codes(q) )</ |
print( Strip_Control_and_Extended_Codes(q) )</syntaxhighlight> |
||
<pre> !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€�‚ƒ„…†‡ˆ‰Š‹Œ�Ž��‘’“”•–—˜™š›œ�žŸ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ |
<pre> !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~€�‚ƒ„…†‡ˆ‰Š‹Œ�Ž��‘’“”•–—˜™š›œ�žŸ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ |
||
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</pre> |
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</pre> |
||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">stripCtrl[x_]:=StringJoin[Select[Characters[x], |
||
MemberQ[CharacterRange["!","~"]~Join~Characters[FromCharacterCode[Range[128,255]]],#]&]] |
MemberQ[CharacterRange["!","~"]~Join~Characters[FromCharacterCode[Range[128,255]]],#]&]] |
||
stripCtrlExt[x_]:=StringJoin[Select[Characters[x], |
stripCtrlExt[x_]:=StringJoin[Select[Characters[x], |
||
MemberQ[CharacterRange["!","~"],#]&]]</ |
MemberQ[CharacterRange["!","~"],#]&]]</syntaxhighlight> |
||
Test: |
Test: |
||
Line 1,521: | Line 1,521: | ||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab"> function str = stripped(str) |
||
str = str(31<str & str<127); |
str = str(31<str & str<127); |
||
end; </ |
end; </syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">proc stripped(str: string): string = |
||
result = "" |
result = "" |
||
for c in str: |
for c in str: |
||
Line 1,539: | Line 1,539: | ||
echo strippedControl "\ba\x00b\n\rc\fdÄ" |
echo strippedControl "\ba\x00b\n\rc\fdÄ" |
||
echo stripped "\ba\x00b\n\rc\fd\xc3"</ |
echo stripped "\ba\x00b\n\rc\fd\xc3"</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>abcdÄ |
<pre>abcdÄ |
||
Line 1,546: | Line 1,546: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let is_control_code c = |
||
let d = int_of_char c in |
let d = int_of_char c in |
||
d < 32 || d = 127 |
d < 32 || d = 127 |
||
Line 1,578: | Line 1,578: | ||
print_endline (strip is_control_code s); |
print_endline (strip is_control_code s); |
||
print_endline (strip (fun c -> (is_control_code c) || (is_extended_char c)) s); |
print_endline (strip (fun c -> (is_control_code c) || (is_extended_char c)) s); |
||
;;</ |
;;</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free_Pascal}} |
{{works with|Free_Pascal}} |
||
< |
<syntaxhighlight lang="pascal">program StripCharacters(output); |
||
function Strip (s: string; control, extended: boolean): string; |
function Strip (s: string; control, extended: boolean): string; |
||
Line 1,607: | Line 1,607: | ||
writeln ('No extnd: ', Strip(test, false, true)); |
writeln ('No extnd: ', Strip(test, false, true)); |
||
writeln ('ASCII: ', Strip(test, true, true)); |
writeln ('ASCII: ', Strip(test, true, true)); |
||
end.</ |
end.</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>% ./StripCharacters |
<pre>% ./StripCharacters |
||
Line 1,620: | Line 1,620: | ||
=={{header|Peloton}}== |
=={{header|Peloton}}== |
||
Peloton has a native instruction for removing control codes from a string, SAL, the Low ASCII Strip. From the manual: |
Peloton has a native instruction for removing control codes from a string, SAL, the Low ASCII Strip. From the manual: |
||
< |
<syntaxhighlight lang="sgml">Create variable with control characters: <@ SAYLETVARLIT>i|This string has control characters |
||
- - - - - - |
- - - - - - |
||
Line 1,627: | Line 1,627: | ||
Assign infix <@ LETVARSALVAR>j|i</@> <@ SAYVAR>j</@> |
Assign infix <@ LETVARSALVAR>j|i</@> <@ SAYVAR>j</@> |
||
Assign prepend <@ LETSALVARVAR>k|i</@> <@ SAYVAR>k</@> |
Assign prepend <@ LETSALVARVAR>k|i</@> <@ SAYVAR>k</@> |
||
Reflexive assign <@ ACTSALVAR>i</@> <@ SAYVAR>i</@></ |
Reflexive assign <@ ACTSALVAR>i</@> <@ SAYVAR>i</@></syntaxhighlight> |
||
Peloton also has SAH, High ASCII Strip. Again, from the manual: |
Peloton also has SAH, High ASCII Strip. Again, from the manual: |
||
< |
<syntaxhighlight lang="sgml">Create variable with high and low ANSI: <@ SAYLETVARLIT>i|This string has both low ansi and high ansi characters - il doit d'être prévenu</@> |
||
Strip high ANSI <@ SAYSAHVAR>i</@> |
Strip high ANSI <@ SAYSAHVAR>i</@> |
||
Assign infix <@ LETVARSAHVAR>j|i</@> <@ SAYVAR>j</@> |
Assign infix <@ LETVARSAHVAR>j|i</@> <@ SAYVAR>j</@> |
||
Assign prepend <@ LETSAHVARVAR>k|i</@> <@ SAYVAR>k</@> |
Assign prepend <@ LETSAHVARVAR>k|i</@> <@ SAYVAR>k</@> |
||
Reflexive assign <@ ACTSAHVAR>i</@> <@ SAYVAR>i</@></ |
Reflexive assign <@ ACTSAHVAR>i</@> <@ SAYVAR>i</@></syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl -w |
||
use strict ; |
use strict ; |
||
Line 1,655: | Line 1,655: | ||
print "\nWithout extended: " ; |
print "\nWithout extended: " ; |
||
print join( '' , map { chr( $_ ) } @noextended ) ; |
print join( '' , map { chr( $_ ) } @noextended ) ; |
||
print "\n" ;</ |
print "\n" ;</syntaxhighlight> |
||
Output: |
Output: |
||
<PRE>before sanitation : �L08&YH�O��n)�:���O�G$���.���"zO���Q�?�� |
<PRE>before sanitation : �L08&YH�O��n)�:���O�G$���.���"zO���Q�?�� |
||
Line 1,667: | Line 1,667: | ||
to build a new one character-by-character.<br> |
to build a new one character-by-character.<br> |
||
I credited Ada solely for the sensible fromch / toch / abovech idea. |
I credited Ada solely for the sensible fromch / toch / abovech idea. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (param default fixes in pwa/p2js)</span> |
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (param default fixes in pwa/p2js)</span> |
||
Line 1,690: | Line 1,690: | ||
<span style="color: #000000;">put_line</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"No Control Chars:"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">filter_it</span><span style="color: #0000FF;">(</span><span style="color: #000000;">full</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- default values for fromch, toch, and abovech</span> |
<span style="color: #000000;">put_line</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"No Control Chars:"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">filter_it</span><span style="color: #0000FF;">(</span><span style="color: #000000;">full</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- default values for fromch, toch, and abovech</span> |
||
<span style="color: #000000;">put_line</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"\" and no Extended:"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">filter_it</span><span style="color: #0000FF;">(</span><span style="color: #000000;">full</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">abovech</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">#FF</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- defaults for fromch and toch</span> |
<span style="color: #000000;">put_line</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"\" and no Extended:"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">filter_it</span><span style="color: #0000FF;">(</span><span style="color: #000000;">full</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">abovech</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">#FF</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- defaults for fromch and toch</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
(desktop/Phix, in a grubby Windows console) |
(desktop/Phix, in a grubby Windows console) |
||
Line 1,700: | Line 1,700: | ||
(pwa/p2js) |
(pwa/p2js) |
||
<pre> |
<pre> |
||
The full string: " |
The full string: " abcédef�", Length:10 |
||
No Control Chars: " abcédef", Length:8 |
No Control Chars: " abcédef", Length:8 |
||
" and no Extended: " abcdef", Length:7 |
" and no Extended: " abcdef", Length:7 |
||
Line 1,709: | Line 1,709: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Control characters in strings are written with a hat (^) in PicoLisp. ^? is the DEL character. |
Control characters in strings are written with a hat (^) in PicoLisp. ^? is the DEL character. |
||
< |
<syntaxhighlight lang="picolisp">(de stripCtrl (Str) |
||
(pack |
(pack |
||
(filter |
(filter |
||
Line 1,720: | Line 1,720: | ||
(filter |
(filter |
||
'((C) (> "^?" C "^_")) |
'((C) (> "^?" C "^_")) |
||
(chop Str) ) ) )</ |
(chop Str) ) ) )</syntaxhighlight> |
||
Test: |
Test: |
||
<pre>: (char "^?") |
<pre>: (char "^?") |
||
Line 1,735: | Line 1,735: | ||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike">> string input = random_string(100); |
||
> (string)((array)input-enumerate(32)-enumerate(255-126,1,127)); |
> (string)((array)input-enumerate(32)-enumerate(255-126,1,127)); |
||
Result: "p_xx08M]cK<FHgR3\\I.x>)Tm<VgakYddy&P7"</ |
Result: "p_xx08M]cK<FHgR3\\I.x>)Tm<VgakYddy&P7"</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang="pl/i"> |
|||
<lang PL/I> |
|||
stripper: proc options (main); |
stripper: proc options (main); |
||
declare s character (100) varying; |
declare s character (100) varying; |
||
Line 1,806: | Line 1,806: | ||
end stripper; |
end stripper; |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 1,816: | Line 1,816: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function Remove-Character |
function Remove-Character |
||
{ |
{ |
||
Line 1,864: | Line 1,864: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
$test = "$([char]9)Français." |
$test = "$([char]9)Français." |
||
Line 1,873: | Line 1,873: | ||
"Extended characters stripped : `"$($test | Remove-Character -Extended)`"" |
"Extended characters stripped : `"$($test | Remove-Character -Extended)`"" |
||
"Control & extended stripped : `"$($test | Remove-Character)`"" |
"Control & extended stripped : `"$($test | Remove-Character)`"" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,881: | Line 1,881: | ||
Control & extended stripped : "Franais." |
Control & extended stripped : "Franais." |
||
</pre> |
</pre> |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
"Français", "Čeština" | Remove-Character -Extended |
"Français", "Čeština" | Remove-Character -Extended |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 1,891: | Line 1,891: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">Procedure.s stripControlCodes(source.s) |
||
Protected i, *ptrChar.Character, length = Len(source), result.s |
Protected i, *ptrChar.Character, length = Len(source), result.s |
||
*ptrChar = @source |
*ptrChar = @source |
||
Line 1,928: | Line 1,928: | ||
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input() |
||
CloseConsole() |
CloseConsole() |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
Sample output: |
Sample output: |
||
<pre>»╫=┐C─≡G(═ç╤â√╝÷╔¬ÿ▌x è4∞|)ï└⌐ƒ9²òτ┌ºáj)▓<~-vPÿφQ╨ù¿╖îFh"[ü╗dÉ₧q#óé├p╫■ |
<pre>»╫=┐C─≡G(═ç╤â√╝÷╔¬ÿ▌x è4∞|)ï└⌐ƒ9²òτ┌ºáj)▓<~-vPÿφQ╨ù¿╖îFh"[ü╗dÉ₧q#óé├p╫■ |
||
Line 1,935: | Line 1,935: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python">stripped = lambda s: "".join(i for i in s if 31 < ord(i) < 127) |
||
print(stripped("\ba\x00b\n\rc\fd\xc3"))</ |
print(stripped("\ba\x00b\n\rc\fd\xc3"))</syntaxhighlight>Output:<syntaxhighlight lang="text">abcd</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
;; Works on both strings (Unicode) and byte strings (raw/ASCII) |
;; Works on both strings (Unicode) and byte strings (raw/ASCII) |
||
Line 1,948: | Line 1,948: | ||
(define (strip-controls-and-extended str) |
(define (strip-controls-and-extended str) |
||
(regexp-replace* #rx"[^\040-\176]+" str "")) |
(regexp-replace* #rx"[^\040-\176]+" str "")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 1,954: | Line 1,954: | ||
{{works with|Rakudo|2018.03}} |
{{works with|Rakudo|2018.03}} |
||
<lang |
<syntaxhighlight lang="raku" line>my $str = (0..400).roll(80)».chr.join; |
||
say $str; |
say $str; |
||
say $str.subst(/<:Cc>/, '', :g); # unicode property: control character |
say $str.subst(/<:Cc>/, '', :g); # unicode property: control character |
||
say $str.subst(/<-[\ ..~]>/, '', :g);</ |
say $str.subst(/<-[\ ..~]>/, '', :g);</syntaxhighlight> |
||
<pre>kşaNĹĭŗ�|Ęw���"ÄlĄWł8iCƁę��Ż�¬�5ĎĶ'óü¸'ÍŸ;ŢƐ¦�´ŷQċűÒŴ$ÃŅ�Đįð+=ĥƂ+Ōĭħ¼ŕc¤H~ìïēÕ |
<pre>kşaNĹĭŗ�|Ęw���"ÄlĄWł8iCƁę��Ż�¬�5ĎĶ'óü¸'ÍŸ;ŢƐ¦�´ŷQċűÒŴ$ÃŅ�Đįð+=ĥƂ+Ōĭħ¼ŕc¤H~ìïēÕ |
||
kşaNĹĭŗ|Ęw"ÄlĄWł8iCƁ꯬5ĎĶ'óü¸'ÍŸ;ŢƐ¦´ŷQċűÒŴ$ÃŅĐįð+=ĥƂ+Ōĭħ¼ŕc¤H~ìïēÕ |
kşaNĹĭŗ|Ęw"ÄlĄWł8iCƁ꯬5ĎĶ'óü¸'ÍŸ;ŢƐ¦´ŷQċűÒŴ$ÃŅĐįð+=ĥƂ+Ōĭħ¼ŕc¤H~ìïēÕ |
||
Line 1,967: | Line 1,967: | ||
===idiomatic version=== |
===idiomatic version=== |
||
This REXX version processes each character in an idiomatic way (if it's a wanted character, then keep it). |
This REXX version processes each character in an idiomatic way (if it's a wanted character, then keep it). |
||
< |
<syntaxhighlight lang="rexx">/*REXX program strips all "control codes" from a character string (ASCII or EBCDIC). */ |
||
z= 'string of ☺☻♥♦⌂, may include control characters and other ♫☼§►↔◄░▒▓█┌┴┐±÷²¬└┬┘ilk.' |
z= 'string of ☺☻♥♦⌂, may include control characters and other ♫☼§►↔◄░▒▓█┌┴┐±÷²¬└┬┘ilk.' |
||
@=' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~' |
@=' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~' |
||
Line 1,976: | Line 1,976: | ||
say 'old = »»»'z"«««" /*add ««fence»» before & after old text*/ |
say 'old = »»»'z"«««" /*add ««fence»» before & after old text*/ |
||
say 'new = »»»'$"«««" /* " " " " " new " */</ |
say 'new = »»»'$"«««" /* " " " " " new " */</syntaxhighlight> |
||
{{out|output}} |
{{out|output}} |
||
<pre> |
<pre> |
||
Line 1,989: | Line 1,989: | ||
Because there are (or should be) fewer unwanted characters than wanted characters, this version is faster. |
Because there are (or should be) fewer unwanted characters than wanted characters, this version is faster. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program strips all "control codes" from a character string (ASCII or EBCDIC). */ |
||
x= 'string of ☺☻♥♦⌂, may include control characters and other ♫☼§►↔◄░▒▓█┌┴┐±÷²¬└┬┘ilk.' |
x= 'string of ☺☻♥♦⌂, may include control characters and other ♫☼§►↔◄░▒▓█┌┴┐±÷²¬└┬┘ilk.' |
||
@=' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghij' || , |
@=' !"#$%&''()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghij' || , |
||
Line 1,999: | Line 1,999: | ||
say 'old = »»»' || x || "«««" /*add ««fence»» before & after old text*/ |
say 'old = »»»' || x || "«««" /*add ««fence»» before & after old text*/ |
||
say 'new = »»»' || $ || "«««" /* " " " " " new " */</ |
say 'new = »»»' || $ || "«««" /* " " " " " new " */</syntaxhighlight> |
||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br> |
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br> |
||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
s = char(31) + "abc" + char(13) + "def" + char(11) + "ghi" + char(10) |
s = char(31) + "abc" + char(13) + "def" + char(11) + "ghi" + char(10) |
||
see strip(s) + nl |
see strip(s) + nl |
||
Line 2,016: | Line 2,016: | ||
next |
next |
||
return strip |
return strip |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">class String |
||
def strip_control_characters() |
def strip_control_characters() |
||
chars.each_with_object("") do |char, str| |
chars.each_with_object("") do |char, str| |
||
Line 2,035: | Line 2,035: | ||
p s = "\ba\x00b\n\rc\fd\xc3\x7ffoo" |
p s = "\ba\x00b\n\rc\fd\xc3\x7ffoo" |
||
p s.strip_control_characters |
p s.strip_control_characters |
||
p s.strip_control_and_extended_characters</ |
p s.strip_control_and_extended_characters</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,043: | Line 2,043: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">s$ = chr$(31) + "abc" + chr$(13) + "def" + chr$(11) + "ghi" + chr$(10) |
||
print strip$(s$) |
print strip$(s$) |
||
Line 2,063: | Line 2,063: | ||
end if |
end if |
||
next i |
next i |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
<pre> |
<pre> |
||
input : chr$(31)+"abc"+chr$(13)+"def"+chr$(11)+"ghi"+chr$(10) |
input : chr$(31)+"abc"+chr$(13)+"def"+chr$(11)+"ghi"+chr$(10) |
||
Line 2,070: | Line 2,070: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
===ASCII: Using StringOps Class=== |
===ASCII: Using StringOps Class=== |
||
< |
<syntaxhighlight lang="scala">val controlCode : (Char) => Boolean = (c:Char) => (c <= 32 || c == 127) |
||
val extendedCode : (Char) => Boolean = (c:Char) => (c <= 32 || c > 127) |
val extendedCode : (Char) => Boolean = (c:Char) => (c <= 32 || c > 127) |
||
Line 2,081: | Line 2,081: | ||
println( "ctrl and extended filtered out: \n\n" + |
println( "ctrl and extended filtered out: \n\n" + |
||
teststring.filterNot(controlCode).filterNot(extendedCode) + "\n" )</ |
teststring.filterNot(controlCode).filterNot(extendedCode) + "\n" )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>ctrl filtered out: |
<pre>ctrl filtered out: |
||
Line 2,097: | Line 2,097: | ||
===Unicode: Using Regular Expressions=== |
===Unicode: Using Regular Expressions=== |
||
<syntaxhighlight lang="scala">// |
|||
<lang Scala>// |
|||
// A Unicode test string |
// A Unicode test string |
||
// |
// |
||
Line 2,116: | Line 2,116: | ||
val htmlNoExtCode = for( i <- sNoExtCode.indices ) yield |
val htmlNoExtCode = for( i <- sNoExtCode.indices ) yield |
||
"&#" + sNoExtCode(i).toInt + ";" + (if( (i+1) % 10 == 0 ) "\n" else "") |
"&#" + sNoExtCode(i).toInt + ";" + (if( (i+1) % 10 == 0 ) "\n" else "") |
||
println( "ctrl and extended filtered out: <br/><br/>\n\n" + htmlNoExtCode.mkString + "<br/><br/>\n" )</ |
println( "ctrl and extended filtered out: <br/><br/>\n\n" + htmlNoExtCode.mkString + "<br/><br/>\n" )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>ctrl filtered out: |
<pre>ctrl filtered out: |
||
Line 2,156: | Line 2,156: | ||
Unicode characters with UTF-8 encoding to the console. |
Unicode characters with UTF-8 encoding to the console. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "utf8.s7i"; |
include "utf8.s7i"; |
||
Line 2,206: | Line 2,206: | ||
writeln("Stripped of control codes and extended characters:"); |
writeln("Stripped of control codes and extended characters:"); |
||
writeln(stripControlAndExtended(src)); |
writeln(stripControlAndExtended(src)); |
||
end func;</ |
end func;</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,221: | Line 2,221: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">var str = "\ba\x00b\n\rc\fd\xc3\x7ffoo" |
||
var letters = str.chars.map{.ord} |
var letters = str.chars.map{.ord} |
||
Line 2,230: | Line 2,230: | ||
var noextended = nocontrols.grep{ _ < 127 } |
var noextended = nocontrols.grep{ _ < 127 } |
||
say noextended.map{.chr}.join.dump</ |
say noextended.map{.chr}.join.dump</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,239: | Line 2,239: | ||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
< |
<syntaxhighlight lang="sml">(* string -> string *) |
||
val stripCntrl = concat o String.tokens Char.isCntrl |
val stripCntrl = concat o String.tokens Char.isCntrl |
||
(* string -> string *) |
(* string -> string *) |
||
val stripCntrlAndExt = concat o String.tokens (not o Char.isPrint)</ |
val stripCntrlAndExt = concat o String.tokens (not o Char.isPrint)</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc stripAsciiCC str { |
||
regsub -all {[\u0000-\u001f\u007f]+} $str "" |
regsub -all {[\u0000-\u001f\u007f]+} $str "" |
||
} |
} |
||
proc stripCC str { |
proc stripCC str { |
||
regsub -all {[^\u0020-\u007e]+} $str "" |
regsub -all {[^\u0020-\u007e]+} $str "" |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TI-83 BASIC}}== |
=={{header|TI-83 BASIC}}== |
||
Line 2,258: | Line 2,258: | ||
The following "normal characters" do exist, but can't be typed on the calculator and a hex editor must be used to enter them: |
The following "normal characters" do exist, but can't be typed on the calculator and a hex editor must be used to enter them: |
||
<lang |
<syntaxhighlight lang="ti83b">#$&@;_`abcdefghijklmnopqrstuvwxyz|~</syntaxhighlight> |
||
The double quote character (ASCII decimal 34) can be entered, but cannot be escaped and thus cannot be stored to strings without the use of hex editors. The following program will remove double quotes from the input string if they were hacked in simply because having one stored to the "check" string is syntactically invalid. |
The double quote character (ASCII decimal 34) can be entered, but cannot be escaped and thus cannot be stored to strings without the use of hex editors. The following program will remove double quotes from the input string if they were hacked in simply because having one stored to the "check" string is syntactically invalid. |
||
Line 2,264: | Line 2,264: | ||
So, in sum, you have to hack the calculator to enter in this program, but once it's entered you can transfer it to unhacked calculators and it will work. |
So, in sum, you have to hack the calculator to enter in this program, but once it's entered you can transfer it to unhacked calculators and it will work. |
||
< |
<syntaxhighlight lang="ti83b">:" !#$%&'()*+,-./0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"→Str0 |
||
:Input ">",Str1 |
:Input ">",Str1 |
||
:":"+Str1+":"→Str1 |
:":"+Str1+":"→Str1 |
||
Line 2,272: | Line 2,272: | ||
:End |
:End |
||
:sub(Str1,2,length(Str1)-1)→Str1 |
:sub(Str1,2,length(Str1)-1)→Str1 |
||
:Pause Str1</ |
:Pause Str1</syntaxhighlight> |
||
=={{header|TXR}}== |
=={{header|TXR}}== |
||
Line 2,278: | Line 2,278: | ||
{{trans|Racket}} |
{{trans|Racket}} |
||
< |
<syntaxhighlight lang="txrlisp">(defun strip-controls (str) |
||
(regsub #/[\x0-\x1F\x7F]+/ "" str)) |
(regsub #/[\x0-\x1F\x7F]+/ "" str)) |
||
(defun strip-controls-and-extended (str) |
(defun strip-controls-and-extended (str) |
||
(regsub #/[^\x20-\x7F]+/ "" str))</ |
(regsub #/[^\x20-\x7F]+/ "" str))</syntaxhighlight> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
Derived from the BASIC version. |
Derived from the BASIC version. |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Function StripCtrlCodes(s) |
Function StripCtrlCodes(s) |
||
tmp = "" |
tmp = "" |
||
Line 2,313: | Line 2,313: | ||
WScript.StdOut.Write "ab�cd�ef�gh�ij†klð€" & " = " & StripCtrlCodesExtChrs("ab�cd�ef�gh�ij†klð€") |
WScript.StdOut.Write "ab�cd�ef�gh�ij†klð€" & " = " & StripCtrlCodesExtChrs("ab�cd�ef�gh�ij†klð€") |
||
WScript.StdOut.WriteLine |
WScript.StdOut.WriteLine |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
Line 2,323: | Line 2,323: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|AutoHotkey}} |
{{trans|AutoHotkey}} |
||
< |
<syntaxhighlight lang="vlang">fn main() { |
||
println(stripped("\ba\x00b\n\rc\fd\xc3")) |
println(stripped("\ba\x00b\n\rc\fd\xc3")) |
||
} |
} |
||
Line 2,331: | Line 2,331: | ||
for value in source {if value > 31 && value < 128 {result += value.ascii_str()}} |
for value in source {if value > 31 && value < 128 {result += value.ascii_str()}} |
||
return result |
return result |
||
}</ |
}</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
Line 2,340: | Line 2,340: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-pattern}} |
{{libheader|Wren-pattern}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/pattern" for Pattern |
||
var s = "\t\n\r\x01\0\fabc\v\v\b\a\x1f\x7f🌇Páez😃É" |
var s = "\t\n\r\x01\0\fabc\v\v\b\a\x1f\x7f🌇Páez😃É" |
||
Line 2,353: | Line 2,353: | ||
r = p.replaceAll(s, "") |
r = p.replaceAll(s, "") |
||
System.print("%(r) -> length %(r.count)") |
System.print("%(r) -> length %(r.count)") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 2,362: | Line 2,362: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">include c:\cxpl\codes; \intrinsic 'code' declarations |
||
string 0; \use zero-terminated string convention |
string 0; \use zero-terminated string convention |
||
Line 2,390: | Line 2,390: | ||
Strip(String, true); |
Strip(String, true); |
||
Text(0, String); CrLf(0); |
Text(0, String); CrLf(0); |
||
]</ |
]</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,402: | Line 2,402: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
ASCII |
ASCII |
||
< |
<syntaxhighlight lang="zkl">var ctlCodes=([1..31].pump(String,"toChar") +(127).toChar()); |
||
var extdChars=[127..255].pump(String,"toChar"); |
var extdChars=[127..255].pump(String,"toChar"); |
||
Line 2,410: | Line 2,410: | ||
(test-extdChars).println("<< no extended chars"); |
(test-extdChars).println("<< no extended chars"); |
||
(test-extdChars-ctlCodes).println("<< text"); |
(test-extdChars-ctlCodes).println("<< text"); |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |