procedure Replace(a,str1,str2)
    local result
    a ? {
        result := ""
        while result ||:= tab(find(str1)) || str2 do
            move(*str1)
        result ||:= tab(0)
        }
    return result
end

On 1/19/2011 8:40 PM, Steve Graham wrote:
I saw this on comp.lang.smalltalk.dolphin and what struck me was how wordy it was. They're just trying to replace all occurrences of a substring in a string with another substring.

In my work language (MUMPS), I would do this as follows:
replace(a,str1,str2) ;
        n fnd
s fnd=0 f s fnd=$f(a,str1,fnd) q:'fnd s $e(a,fnd-$l(str1),fnd-1)=str2
        q a

How would you do it in Unicon?


Steve

---

Smalltalk version:
replaceAll: aSubstring with: aReplacementString
        | subStringCollection newString |
        (self findString: aSubstring) < 1
                ifTrue: [^newString := self]
                ifFalse:
                        [newString := String new.
                        subStringCollection := self subStrings:
aSubstring.
                        subStringCollection do: [:eachString |
newString := newString ,
eachString , aReplacementString]].
        ^newString.


------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand
malware threats, the impact they can have on your business, and how you
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl


_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group
------------------------------------------------------------------------------
Protect Your Site and Customers from Malware Attacks
Learn about various malware tactics and how to avoid them. Understand 
malware threats, the impact they can have on your business, and how you 
can protect your company and customers by using code signing.
http://p.sf.net/sfu/oracle-sfdevnl
_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group

Reply via email to