Strip a set of characters from a string: Difference between revisions
Strip a set of characters from a string (view source)
Revision as of 09:46, 15 March 2023
, 1 year ago→{{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
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() {
string chars = "aei";
auto ss = "Sh ws soul strppr. Sh took my hrt!";▼
writeln(stripchars(s, chars));
writeln(stripchars2(s, chars));
}</syntaxhighlight>
{{out}}
Sh ws soul strppr. Sh took my hrt!</pre>
=={{header|Delphi}}==
|