FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Replace Text
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Replace Text
Posted: Wed Oct 11, 2017 05:20 PM

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 16.11, xHarbour 1.2.3, Bcc730)
Posts: 933
Joined: Sun Oct 09, 2005 01:05 PM
Re: Replace Text
Posted: Wed Oct 11, 2017 07:28 PM

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

Thanks,

Jeff Barnes



(FWH 16.11, xHarbour 1.2.3, Bcc730)
Posts: 231
Joined: Fri Jul 20, 2012 01:49 AM
Re: Replace Text
Posted: Thu Oct 12, 2017 12:21 AM
Hi Jeff,

Try this one:

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

Code (fw): Select all Collapse
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

Continue the discussion