Strip a set of characters from a string: Difference between revisions
Strip a set of characters from a string (view source)
Revision as of 16:27, 5 April 2024
, 1 month ago→{{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]")`) |
|||
(12 intermediate revisions by 9 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 1,285 ⟶ 1,296:
=={{header|EasyLang}}==
<syntaxhighlight
func$ strip s$ del$ .
if strpos del$ c$ <> 0
c$ = ""
.
r$ &= c$
.
return r$
Line 1,327 ⟶ 1,331:
var removeChars := "aei";
console.printLine(testString.filterBy::(ch => removeChars.indexOf(0, ch) == -1).summarize(new StringWriter()))
}</syntaxhighlight>
{{out}}
Line 1,350 ⟶ 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,445 ⟶ 1,465:
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
let stripChars text (chars:string) =
Seq.fold (
fun (s:string) c -> s.Replace(c.ToString(),"")
) text
printfn "%s" (stripChars "She was a soul stripper. She took my heart!" "aei")
</syntaxhighlight>
{{out}}
<pre>
Sh ws soul strppr. Sh took my hrt!</pre>
</pre>
=={{header|Factor}}==
Line 1,849 ⟶ 1,869:
Sh ws soul strppr. Sh took my hrt!</pre>
=={{header|
<syntaxhighlight lang="
{{out}}
<pre>"Sh ws soul strppr. Sh took my hrt!"</pre>
=={{header|Kotlin}}==
<syntaxhighlight lang="kotlin">fun stripChars(s: String, r: String) = s.filter { it !in r }
fun main(args: Array<String>) {
Line 2,395 ⟶ 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,545 ⟶ 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,635 ⟶ 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,717 ⟶ 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,920 ⟶ 3,005:
=={{header|Wren}}==
<syntaxhighlight lang="
return s.map { |c|
return (t.indexOf(c) == -1) ? c : ""
|