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

From Rosetta Code
Content added Content deleted
(Added PicoLisp)
(Oops, was wrong task)
Line 35: Line 35:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de trimLeft (Str)
<lang PicoLisp>(de strDiff (Str1 Str2)
(pack (flip (trim (flip (chop Str))))) )
(pack (diff (chop Str1) (chop Str2))) )</lang>
Output:

<pre>: (strDiff "She was a soul stripper. She took my heart!" "aei")
(de trimRight (Str)
-> "Sh ws soul strppr. Sh took my hrt!"</pre>
(pack (trim (chop Str))) )

(de trimBoth (Str)
(pack (clip (chop Str))) )</lang>
Test:
<pre>: (trimLeft " ^G ^I trimmed left ^L ")
-> "trimmed left ^L "

: (trimRight " ^G ^I trimmed right ^L ")
-> " ^G ^I trimmed right"

: (trimBoth " ^G ^I trimmed both ^L ")
-> "trimmed both"</pre>


=={{header|PureBasic}}==
=={{header|PureBasic}}==

Revision as of 12:58, 6 June 2011

Task
Strip a set of characters from a string
You are encouraged to solve this task according to the task description, using any language you may know.

The task is to create a function that strips a set of characters from a string. The function should take two arguments: the first argument being a string to stripped and the second, a string containing the set of characters to be stripped. The returned string should contain the first string, stripped of any characters in the second argument:

<lang pseudocode> print stripchars("She was a soul stripper. She took my heart!","aei") Sh ws soul strppr. Sh took my hrt!</lang>

J

Solution:
The dyadic primitive -. (Less) is probably the simplest way to solve this task.

Example Usage: <lang j> 'She was a soul stripper. She took my heart!' -. 'aei' Sh ws soul strppr. Sh took my hrt!</lang>

OCaml

<lang ocaml>let stripchars s cs =

 let len = String.length s in
 let res = String.create len in
 let rec aux i j =
   if i >= len then String.sub res 0 j
   else if String.contains cs s.[i] then
     aux (succ i) (j)
   else begin
     res.[j] <- s.[i];
     aux (succ i) (succ j)
   end
 in
 aux 0 0</lang>

testing in the toplevel:

# stripchars "She was a soul stripper. She took my heart!" "aei" ;;
- : string = "Sh ws  soul strppr. Sh took my hrt!"

PicoLisp

<lang PicoLisp>(de strDiff (Str1 Str2)

  (pack (diff (chop Str1) (chop Str2))) )</lang>

Output:

: (strDiff "She was a soul stripper. She took my heart!" "aei")
-> "Sh ws  soul strppr. Sh took my hrt!"

PureBasic

PureBasic uses a single (for ASCII) or a two-byte (for Unicode) null to signal the end of a string. Nulls are thus excluded from the allowable characters to strip as they can't be included in a PureBasic string. <lang PureBasic>Procedure.s stripChars(source.s, charsToStrip.s)

 Protected i, *ptrChar.Character, length = Len(source), result.s
 *ptrChar = @source
 For i = 1 To length
   If Not FindString(charsToStrip, Chr(*ptrChar\c))
     result + Chr(*ptrChar\c)
   EndIf
   *ptrChar + SizeOf(Character)
 Next
 ProcedureReturn result 

EndProcedure

If OpenConsole()

 PrintN(stripChars("She was a soul stripper. She took my heart!", "aei"))
 
 Print(#CRLF$ + #CRLF$ + "Press ENTER to exit"): Input()
 CloseConsole()

EndIf</lang> Sample output:

Sh ws  soul strppr. Sh took my hrt!

Python

<lang python>>>> def stripchars(s, chars): ... return "".join(c for c in s if c not in chars) ... >>> stripchars("She was a soul stripper. She took my heart!", "aei") 'Sh ws soul strppr. Sh took my hrt!'</lang>

Tcl

<lang tcl>proc stripchars {str chars} {

   foreach c [split $chars ""] {set str [string map [list $c ""] $str]}
   return $str

}

set s "She was a soul stripper. She took my heart!" puts [stripchars $s "aei"]</lang>