Replace Text

Post Reply
User avatar
Jeff Barnes
Posts: 912
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Replace Text

Post by Jeff Barnes »

Is there a function that will replace text between two tokens?

Example:

cOldString := "[START]some text, some more text[END]"

What I'm looking for is something like:

cNewString := "This is the new text"
cFinalString := ReplaceText( "[START]", "[END]", cOldString, cNewString )

cFinalString would be "[START]This is the new text[END]"

I hope that makes sense.
Thanks,
Jeff Barnes

(FWH 12.01, xHarbour 1.2.1, Bcc582)
User avatar
Jeff Barnes
Posts: 912
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada
Contact:

Re: Replace Text

Post by Jeff Barnes »

I figured it out.
A combination of AT() and STUFF() did the trick.
Thanks,
Jeff Barnes

(FWH 12.01, xHarbour 1.2.1, Bcc582)
User avatar
Lailton
Posts: 99
Joined: Fri Jul 20, 2012 1:49 am
Location: Brazil
Contact:

Re: Replace Text

Post by Lailton »

Hi Jeff,

Try this one:

? GroupBy( "[START]", "[END]", cString,.F.,.F.)

Code: Select all

Function GroupBy( cStart, cEnd, cString, lTags, lBreak )

Local nBegin, nEnd

Local cFound, nAT

Default lTags:=.F., lBreak:=.F.


nBegin := At( cStart, cString )

If(nBegin == 0 ) ; Return "" ; EndIf


If(lTags == .F.) ; nBegin += Len(cStart) ; EndIf


If( cEnd != Nil )

    nEnd := At( cEnd, cString, nBegin )

    If nEnd == 0

        Return ""

    Else

        nEnd := nEnd - nBegin

    EndIf

    If( lTags )

        nEnd += Len( cEnd )

    EndIf

Else

    If lBreak

        nAt := At(CRLF,Substr(cString,nBegin,Len(cString)))

        If nAt > 0

            nEnd := nAt - 1

            If nEnd < Len(cString) ; nEnd := Len(cString)+1 ; Endif

        Else

            nEnd := Len( cString )

        EndIf

    Else        

        nEnd := Len( cString )

    EndIf

EndIf


cFound := Substr( cString, nBegin, nEnd )

If lBreak

    If lTags

        cFound:=StrTran(cFound,CRLF)

    Else

        If cEnd != Nil

            nAt := At(CRLF,cFound)

            If nAt > 0

                cFound := Left(cFound,nAT)

            EndIf

        Endif

    EndIf

EndIf

Return cFound
 
Regards,
Lailton Fernando Mariano
https://www.harbour.ninja
Post Reply