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

→‎{{header|D}}: Replaced obsolete solution. std.string.removechars was removed from D circa 2018.
(→‎{{header|D}}: Replaced obsolete solution. std.string.removechars was removed from D circa 2018.)
Line 1,120:
 
=={{header|D}}==
This example shows both the functional and regex solutions.
<syntaxhighlight lang="d">import std.stdio, std.string;
 
string stripchars(string s, string chars) {
import std.algorithm;
import std.conv;
return s.filter!(c => !chars.count(c)).to!string;
}
 
string stripchars2(string s, string chars) {
import std.array;
import std.regex;
return replaceAll(s, regex(["[", chars, "]"].join("")), "");
}
 
void main() {
autostring s = "She was a soul stripper. She took my heart!";
string chars = "aei";
auto ss = "Sh ws soul strppr. Sh took my hrt!";
 
assert(s.removechars("aei") == ss);
writeln(stripchars(s, chars));
writeln(stripchars2(s, chars));
}</syntaxhighlight>
 
{{out}}
auto ss = "<pre>Sh ws soul strppr. Sh took my hrt!";
Sh ws soul strppr. Sh took my hrt!</pre>
 
=={{header|Delphi}}==