Replace Text

Replace Text

Postby Jeff Barnes » Wed Oct 11, 2017 5: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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Replace Text

Postby Jeff Barnes » Wed Oct 11, 2017 7: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)
User avatar
Jeff Barnes
 
Posts: 929
Joined: Sun Oct 09, 2005 1:05 pm
Location: Ontario, Canada

Re: Replace Text

Postby Lailton » Thu Oct 12, 2017 12:21 am

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
 
Regards,
Lailton Fernando Mariano
User avatar
Lailton
 
Posts: 153
Joined: Fri Jul 20, 2012 1:49 am
Location: Brazil


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 59 guests