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]")`)
 
(16 intermediate revisions by 10 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 941 ⟶ 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,220 ⟶ 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,243 ⟶ 1,331:
var removeChars := "aei";
console.printLine(testString.filterBy::(ch => removeChars.indexOf(0, ch) == -1).summarize(new StringWriter()))
}</syntaxhighlight>
{{out}}
Line 1,266 ⟶ 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,361 ⟶ 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,765 ⟶ 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,311 ⟶ 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,461 ⟶ 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,551 ⟶ 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,633 ⟶ 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,836 ⟶ 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