Strip a set of characters from a string: Difference between revisions

→‎{{header|Kotlin}}: Corrected Kotlin solution: the use of regex is error-prone, e.g. `stripChars("fails to remove ] bracket", "aei]")`
(→‎{{header|Kotlin}}: Corrected Kotlin solution: the use of regex is error-prone, e.g. `stripChars("fails to remove ] bracket", "aei]")`)
 
(20 intermediate revisions by 13 users not shown)
Line 205:
{{out}}
 
<pre>Sh ws soul strppr. Sh took my hrt!</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN s stripchars chs:
PUT "" IN result
FOR c IN s:
IF c not.in chs: PUT result^c IN result
RETURN result
 
WRITE "She was a soul stripper. She took my heart!" stripchars "aei"/</syntaxhighlight>
{{out}}
<pre>Sh ws soul strppr. Sh took my hrt!</pre>
 
Line 389 ⟶ 400:
text: She was a soul stripper. She took my heart!
->: Sh ws soul strppr. Sh took my hrt!
</pre>
 
=={{header|Amazing Hopper}}==
<p>Amazing Hopper! Flavour "Jambo".</p>
<syntaxhighlight lang="Amazing Hopper">
#include <jambo.h>
 
Main
c = ".$%#\tEste@@@ mensaje será purgado!$$$"
 
{"Hopper assembler:\n\n"}
{"$%#@.\t", c} str to utf8, delete char, {"\n"} print
Printnl ("\nHopper Jambo formal syntax:\n\n", Char del ( "$%#@.\t", Utf8( c ) ) )
Set ' "\nHopper Jambo Natural syntax:\n\n", "$%#@.\t", c', Get utf8, and delete char
then print with newline
End
</syntaxhighlight>
{{out}}
<pre>
Hopper assembler:
 
Este mensaje será purgado!
 
Hopper Jambo formal syntax:
 
Este mensaje será purgado!
 
Hopper Jambo Natural syntax:
 
Este mensaje será purgado!
 
</pre>
 
Line 705 ⟶ 749:
{{out}}
Sh ws soul strppr. Sh took my hrt!
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{trans|BASIC}}
<syntaxhighlight lang="qbasic">100 cls
110 print stripchars$("She was a soul stripper. She took my heart!","aei")
120 sub stripchars$(src$,remove$)
130 s$ = src$
140 for l0 = 1 to len(remove$)
150 do
160 t = instr(s$,mid$(remove$,l0,1))
170 if t then
180 s$ = left$(s$,t-1)+mid$(s$,t+1)
190 else
200 exit do
210 endif
220 loop
230 next
240 stripchars$ = s$
250 end sub
260 end</syntaxhighlight>
{{out}}
<pre>Sh ws soul strppr. Sh took my hrt!</pre>
 
==={{header|IS-BASIC}}===
Line 918 ⟶ 985:
return 0;
}</syntaxhighlight>Output same as above.
 
==={{header|Gadget}}===
 
<p>Gadget C-library:
[https://github.com/DanielStuardo/Gadget Gadget C-library in Github]
</p>
<syntaxhighlight lang="c">
#include <gadget/gadget.h>
 
LIB_GADGET_START
 
Main
String r, c = ".$%#\tEste@@@ mensaje será purgado!$$$", set = "$%#@.\t";
Stack{
Store ( r, Char_del( Upper( c ), set ) );
}Stack_off
 
Print "Original = [%s]\nChar deleted = [%s]\n", c, r;
Free secure r,c,set;
End
</syntaxhighlight>
{{out}}
<pre>
Original = [.$%# Este@@@ mensaje será purgado!$$$]
Char deleted = [ESTE MENSAJE SERÁ PURGADO!]
 
</pre>
 
=={{header|C sharp}}==
Line 1,197 ⟶ 1,293:
{{out}}
<pre>Sh ws soul strppr. Sh took my hrt!</pre>
 
=={{header|EasyLang}}==
 
<syntaxhighlight>
func$ strip s$ del$ .
for c$ in strchars s$
if strpos del$ c$ <> 0
c$ = ""
.
r$ &= c$
.
return r$
.
print strip "She was a soul stripper. She took my heart!" "aei"
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 1,220 ⟶ 1,331:
var removeChars := "aei";
console.printLine(testString.filterBy::(ch => removeChars.indexOf(0, ch) == -1).summarize(new StringWriter()))
}</syntaxhighlight>
{{out}}
Line 1,243 ⟶ 1,354:
RC.stripchars(str, "aei")
# => Sh ws soul strppr. Sh took my hrt!</syntaxhighlight>
 
=={{header|Emacs Lisp}}==
<syntaxhighlight lang="lisp">
(defun stripchars (s chars)
(seq-into
(seq-filter (lambda (x) (not (seq-contains chars x))) s)
'string))
 
(stripchars "She was a soul stripper. She took my heart!" "aei")
</syntaxhighlight>
 
{{out}}
 
<pre>
"Sh ws soul strppr. Sh took my hrt!"
</pre>
 
=={{header|Erlang}}==
Line 1,338 ⟶ 1,465:
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">let stripChars text (chars:string) =
let stripChars text (chars:string) =
Array.fold (
Seq.fold (
fun (s:string) c -> s.Replace(c.ToString(),"")
) text (chars.ToCharArray())
printfn "%s" (stripChars "She was a soul stripper. She took my heart!" "aei")
 
</syntaxhighlight>
[<EntryPoint>]
{{out}}
let main args =
<pre>
printfn "%s" (stripChars "She was a soul stripper. She took my heart!" "aei")
Sh ws soul strppr. Sh took my hrt!</pre>
0</syntaxhighlight>
</pre>
Output
<pre>Sh ws soul strppr. Sh took my hrt!</pre>
 
=={{header|Factor}}==
Line 1,472 ⟶ 1,599:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Strip_a_set_of_characters_from_a_string}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Strip a set of characters from a string 01.png]]
In '''[https://formulae.org/?example=Strip_a_set_of_characters_from_a_string this]''' page you can see the program(s) related to this task and their results.
 
'''Test case'''
 
[[File:Fōrmulæ - Strip a set of characters from a string 02.png]]
 
[[File:Fōrmulæ - Strip a set of characters from a string 03.png]]
 
=={{header|FutureBasic}}==
Line 1,590 ⟶ 1,723:
rdvrks r nt trs.
-></pre>
 
=={{header|Insitux}}==
{{Trans|Clojure}}
<syntaxhighlight lang="insitux">(function strip from what
(remove (to-vec what) from))
 
(strip "She was a soul stripper. She took my heart!" "aei")
;returns "Sh ws soul strppr. Sh took my hrt!"</syntaxhighlight>
 
=={{header|J}}==
Line 1,728 ⟶ 1,869:
Sh ws soul strppr. Sh took my hrt!</pre>
 
=={{header|KotlinK}}==
<syntaxhighlight lang="scalak">//"She versionwas 1a soul stripper.0.6 She took my heart!" ^ "aei"</syntaxhighlight>
{{out}}
<pre>"Sh ws soul strppr. Sh took my hrt!"</pre>
 
=={{header|Kotlin}}==
fun stripChars(s: String, r: String) = s.replace(Regex("[$r]"), "")
<syntaxhighlight lang="kotlin">fun stripChars(s: String, r: String) = s.filter { it !in r }
 
fun main(args: Array<String>) {
Line 2,274 ⟶ 2,418:
stripchars: func [str chars] [trim/with str chars]
stripchars "She was a soul stripper. She took my heart!" "aei"</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
= <Prout <Strip ('aei') 'She was a soul stripper. She took my heart!'>>;
};
 
Strip {
(e.Chs) = ;
(e.Chs) s.C e.S, e.Chs: e.1 s.C e.2 = <Strip (e.Chs) e.S>;
(e.Chs) s.C e.S = s.C <Strip (e.Chs) e.S>;
};</syntaxhighlight>
{{out}}
<pre>Sh ws soul strppr. Sh took my hrt!</pre>
 
=={{header|REXX}}==
Line 2,424 ⟶ 2,581:
Log:
<syntaxhighlight lang="sas">Sh ws soul strppr. Sh took my hrt!</syntaxhighlight>
 
=={{header|S-BASIC}}==
<syntaxhighlight lang="BASIC">
rem - strip unwanted characters from a string
function strip(s, unwanted = string) = string
var i = integer
var outstr = string
var ch = char
outstr = ""
for i = 1 to len(s)
ch = mid(s, i, 1)
if instr(1, unwanted, ch) = 0 then
outstr = outstr + ch
next i
end = outstr
 
rem - exercise the routine
print strip("She was a soul stripper. She took my heart!","aei")
 
end
</syntaxhighlight>
{{out}}
<pre>
Sh ws soul strppr. Sh took my hrt!
</pre>
 
=={{header|Scala}}==
Line 2,514 ⟶ 2,696:
Sh ws soul strppr. Sh took my hrt!
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program strip_chars;
print(strip("She was a soul stripper. She took my heart!", "aei"));
 
proc strip(s, chs);
return +/[c : c in s | not c in chs];
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>Sh ws soul strppr. Sh took my hrt!</pre>
 
=={{header|Sidef}}==
Line 2,596 ⟶ 2,789:
<pre>- stripchars ("She was a soul stripper. She took my heart!", "aei") ;
val it = "Sh ws soul strppr. Sh took my hrt!" : string</pre>
 
=={{header|Stringle}}==
<syntaxhighlight lang="stringle">a "She was a soul stripper. She took my heart!"
b "aei"
#a
c c .a
b %.\c #c #:c
a :a
#a
$ c
</syntaxhighlight>
{{out}}
<pre>Sh ws soul strppr. Sh took my hrt!</pre>
 
=={{header|Swift}}==
Line 2,799 ⟶ 3,005:
 
=={{header|Wren}}==
<syntaxhighlight lang="javascriptwren">var stripChars = Fn.new { |s, t|
return s.map { |c|
return (t.indexOf(c) == -1) ? c : ""
44

edits