Page 1 of 1

Replace Text

PostPosted: Wed Oct 11, 2017 5:20 pm
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.

Re: Replace Text

PostPosted: Wed Oct 11, 2017 7:28 pm
by Jeff Barnes
I figured it out.
A combination of AT() and STUFF() did the trick.

Re: Replace Text

PostPosted: Thu Oct 12, 2017 12:21 am
by Lailton
Hi Jeff,

Try this one:

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

Code: Select all  Expand view
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