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

Content added Content deleted
(→‎Tcl: Added implementation (based on REs))
Line 8: Line 8:


On a non-ASCII based system, we consider characters that do not have a corresponding glyph on the ASCII table (within the ASCII range of 32 to 126 decimal) to be an extended character for the purpose of this task.
On a non-ASCII based system, we consider characters that do not have a corresponding glyph on the ASCII table (within the ASCII range of 32 to 126 decimal) to be an extended character for the purpose of this task.

=={{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.
<lang Icon>procedure main(A)
write(image(deletec(&ascii,&ascii--(&ascii)[33:127])))
end
link strings
</lang>

{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/strings.icn strings.icn provides deletec]

The IPL procedure ''deletec'' is equivalent to this:
<lang Icon>procedure deletec(s, c) #: delete characters
result := ""
s ? {
while result ||:= tab(upto(c)) do tab(many(c))
return result ||:= tab(0)
}
end</lang>


Output:<pre>" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}"</pre>


=={{header|J}}==
=={{header|J}}==