Paste: Replace occurences of string in string with string

Author: paldepind
Mode: factor
Date: Thu, 3 Jan 2013 20:03:45
Plain Text |
! This word takes three string from the stack, finds all occurences
! of "math" in "string" and replaces them with "replace"

: replace-string ( string match replace -- string' )
    '[ [ _ ?head [ _ % ] [ [ first , ] [ rest ] bi ] if
        dup empty? not ] loop ] "" make swap drop ;

! For instance
"cheesecake" "cake" "pie" replace-string
! Leaves "cheesepie" on the stack

Annotation: replace-string2

Author: paldepind
Mode: factor
Date: Thu, 3 Jan 2013 20:32:01
Plain Text |
: replace-string2 ( string match replace -- string' )
    '[
       [ dup empty? ]
       [ _ ?head [ _ % ] [ [ first , ] [ rest ] bi ] if ]
       until
    ] "" make nip ;

New Annotation

Summary:
Author:
Mode:
Body: