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]")`
(Adds slope example) |
(→{{header|Kotlin}}: Corrected Kotlin solution: the use of regex is error-prone, e.g. `stripChars("fails to remove ] bracket", "aei]")`) |
||
(27 intermediate revisions by 18 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,120 ⟶ 1,216:
=={{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.regex;
return replaceAll(s, regex("[" ~ chars ~ "]"), "");
}
void main() {
string chars = "aei";
writeln(stripchars(s, chars));
writeln(stripchars2(s, chars));
}</syntaxhighlight>
{{out}}
<pre>Sh ws soul strppr. Sh took my hrt!
Sh ws soul strppr. Sh took my hrt!</pre>
=={{header|Delphi}}==
Line 1,179 ⟶ 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,202 ⟶ 1,331:
var removeChars := "aei";
console.printLine(testString.filterBy::(ch => removeChars.indexOf(0, ch) == -1).summarize(new StringWriter()))
}</syntaxhighlight>
{{out}}
Line 1,225 ⟶ 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,320 ⟶ 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,454 ⟶ 1,599:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Strip_a_set_of_characters_from_a_string}}
'''Solution'''
[[File:Fōrmulæ - Strip a set of characters from a string 01.png]]
'''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}}==
<syntaxhighlight lang="futurebasic">window 1, @"Strip a set of characters from a string"
local fn StringByStrippingCharacters( string as CFStringRef, chrs as CFStringRef ) as CFStringRef
end fn = fn ArrayComponentsJoinedByString( fn StringComponentsSeparatedByCharactersInSet( string, fn CharacterSetWithCharactersInString( chrs ) ), @"" )
CFStringRef string, chrs
string = @"She was a soul stripper. She took my heart!"
chrs = @"aei"
print string
print fn StringByStrippingCharacters( string, chrs )
HandleEvents</syntaxhighlight>
{{out}}
<pre>
She was a soul stripper. She took my heart!
Sh ws soul strppr. Sh took my hrt!
</pre>
=={{header|Gambas}}==
Line 1,551 ⟶ 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,560 ⟶ 1,740:
=={{header|Java}}==
The most basic approach is to use the ''String.replace'' method.
<syntaxhighlight lang="java">
String stripCharacters(String string, String characters) {
for (char character : characters.toCharArray())
string = string.replace(String.valueOf(character), "");
return string;
}
</syntaxhighlight>
You could also use a ''StringBuilder'' which provides a ''deleteCharAt'' method.
<syntaxhighlight lang="java">
String stripCharacters(String string, String characters) {
StringBuilder stripped = new StringBuilder(string);
/* traversing the string backwards is necessary to avoid collision */
for (int index = string.length() - 1; index >= 0; index--) {
if (characters.contains(String.valueOf(string.charAt(index))))
stripped.deleteCharAt(index);
}
return stripped.toString();
}
</syntaxhighlight>
You could use the ''String.replaceAll'' method, which takes a regular expression as it's first argument.
<syntaxhighlight lang="java">
static String stripCharacters(String string, String characters) {
/* be sure to 'quote' the 'characters' to avoid pattern collision */
characters = Pattern.quote(characters);
string = string.replaceAll("[%s]".formatted(characters), "");
return string;
}
</syntaxhighlight>
These will all produce the following string.
<pre>
Sh ws soul strppr. Sh took my hrt!
</pre>
=={{header|JavaScript}}==
Line 1,647 ⟶ 1,843:
{{Out}}
<pre>'Sh ws soul strppr. Sh took my hrt!'</pre>
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE stripchars == [in not] cons filter.
"She was a soul stripper. She took my heart!" "aei" stripchars.</syntaxhighlight>
{{out}}
<pre>"Sh ws soul strppr. Sh took my hrt!"</pre>
=={{header|jq}}==
Line 1,666 ⟶ 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,212 ⟶ 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,295 ⟶ 2,514:
return cList
</syntaxhighlight>
=={{header|RPL}}==
≪ → string out
≪ "" 1 string SIZE '''FOR''' j
string j DUP SUB
'''IF''' out OVER POS '''THEN''' DROP '''ELSE''' + '''END'''
'''NEXT'''
≫ ≫ 'STRIP' STO
{{in}}
<pre>
"She was a soul stripper. She took my heart!" "aei" STRIP
</pre>
{{in}}
<pre>
1: "Sh ws soul strppr. Sh took my hrt!"
</pre>
=={{header|Ruby}}==
Line 2,346 ⟶ 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,436 ⟶ 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,518 ⟶ 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,704 ⟶ 2,988:
<pre>Sh ws soul strppr. Sh took my hrt!</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn main() {
println(stripchars("She was a soul stripper. She took my heart!","aei"))
}
Line 2,721 ⟶ 3,005:
=={{header|Wren}}==
<syntaxhighlight lang="
return s.map { |c|
return (t.indexOf(c) == -1) ? c : ""
|