FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Searching string
Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Searching string
Posted: Wed Sep 05, 2012 12:48 PM

Hi,

How i can get a initial and final position of a word in a memo text without space?

Like this:

<cMun>TESTE<cMun><cCod>01213<cCod>AAAAAAAAAAAAAAAAAAAAA

In this case <cCod> starts in 18 and ending 23

Thanks.

Posts: 1091
Joined: Thu Nov 17, 2005 11:08 AM
Re: Searching string
Posted: Wed Sep 05, 2012 01:06 PM
Code (fw): Select all Collapse
#include "fivewin.ch"

FUNCTION MAIN()
//                123456789012345678901234567890123456789012345678901234567890
LOCAL cString := "<cMun>TESTE<cMun><cCod>01213<cCod>AAAAAAAAAAAAAAAAAAAAA"
LOCAL cWord   := "<cCod>"
LOCAL aPos    := {}

aPos :=  mypos( cString , cWord )
? aPos[1]
? aPos[2]

RETURN NIL

FUNCTION MYPOS( cString , cWord )
LOCAL nStart , nEnd

nStart :=  AT( cWord , cString )  // use RAT for last for right to left search

nEnd   :=  nStart + LEN( cWord ) - 1
RETURN { nStart , nEnd }


I find "<cCod>" 2 times in this string
Marco Boschi
info@marcoboschi.it
Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Searching string
Posted: Wed Sep 05, 2012 01:39 PM
MarcoBoschi wrote:
Code (fw): Select all Collapse
#include "fivewin.ch"

FUNCTION MAIN()
//                123456789012345678901234567890123456789012345678901234567890
LOCAL cString := "<cMun>TESTE<cMun><cCod>01213<cCod>AAAAAAAAAAAAAAAAAAAAA"
LOCAL cWord   := "<cCod>"
LOCAL aPos    := {}

aPos :=  mypos( cString , cWord )
? aPos[1]
? aPos[2]

RETURN NIL

FUNCTION MYPOS( cString , cWord )
LOCAL nStart , nEnd

nStart :=  AT( cWord , cString )  // use RAT for last for right to left search

nEnd   :=  nStart + LEN( cWord ) - 1
RETURN { nStart , nEnd }


I find "<cCod>" 2 times in this string


Thanks for your answer, this is the problem i want to know all <cCod> positions.
Posts: 1091
Joined: Thu Nov 17, 2005 11:08 AM
Re: Searching string
Posted: Wed Sep 05, 2012 03:34 PM

include "fivewin.ch"

FUNCTION MAIN()
// 1234567890123456789012345678901234567890123456789012345
LOCAL cString := "<cMun>TESTE<cMun><cCod>01213<cCod>AAAAAAAAAAAAAAAAAAAAA"
LOCAL cWord := "<cCod>"
LOCAL aPos := {}
LOCAL i

aPos := mypos( cString , cWord )
FOR i := 1 TO LEN( aPos )
? aPos[ i , 1 ] , aPos[ i , 2 ]
NEXT i

RETURN NIL

FUNCTION MYPOS( cString , cWord )

LOCAL aPos := {}
LOCAL i := 1

DO WHILE .T.

IF SUBSTR( cString , i , LEN( cWord ) ) = cWord
AADD( aPos , { i , i + LEN( cWord ) - 1 } )
i := i + LEN( cWord ) -1
ENDIF

IF i >= LEN( cString )
EXIT
ENDIF
i ++

ENDDO
RETURN aPos

Marco Boschi
info@marcoboschi.it
Posts: 1091
Joined: Thu Nov 17, 2005 11:08 AM
Re: Searching string
Posted: Wed Sep 05, 2012 03:38 PM
Code (fw): Select all Collapse
#include "fivewin.ch"

FUNCTION MAIN()
//                1234567890123456789012345678901234567890123456789012345
LOCAL cString := "<cMun>TESTE<cMun><cCod>01213<cCod>AAAAAAAAAAAAAAAAAAAAA"
LOCAL cWord   := "<cCod>"
LOCAL aPos    := {}
LOCAL i

aPos :=  mypos( cString , cWord )
FOR i := 1 TO LEN( aPos )
    ? aPos[ i , 1 ]  , aPos[ i , 2 ]
NEXT i

RETURN NIL

FUNCTION MYPOS( cString , cWord )

LOCAL aPos := {}
LOCAL i := 1

DO WHILE .T.

   IF SUBSTR( cString , i , LEN( cWord ) ) = cWord
      AADD( aPos , { i , i + LEN( cWord ) - 1  } )
      i := i + LEN( cWord ) -1
   ENDIF

   IF i >= LEN( cString )
      EXIT
   ENDIF
   i ++

ENDDO
RETURN aPos
Marco Boschi
info@marcoboschi.it
Posts: 824
Joined: Thu Oct 13, 2005 07:39 AM
Re: Searching string
Posted: Thu Sep 06, 2012 07:15 AM
Have a look at the function NumAt () from ct.lib

NumAt()
Counts multiple occurrences of a substring within a string.
Syntax
NumAt( <cSearch> , ;
<cString> , ;
[<nSkipChars>] ) --> nCount

Arguments
<cSearch> This is a character string to search for in <cString>.
<cString> This is the character string to find <cSearch> in.
<nSkipChars> This numeric parameter defaults to 0 so that the function begins searching with the first character of <cString>. Passing a value > 0 instructs the function to ignore the first <nSkipChars> characters in the search. Return
The function returns the number of times <cSearch> is found in <cString> as a numeric value.
kind regards

Stefan
Posts: 65
Joined: Fri Feb 13, 2009 12:03 PM
Re: Searching string
Posted: Mon Sep 10, 2012 01:27 AM

You can also adapt below code:

include "fivewin.ch"

FUNCTION MAIN()
// 1234567890123456789012345678901234567890123456789012345
LOCAL cString := "<cMun>TESTE<cMun><cCod>01213<cCod>AAAAAAAAAAAAAAAAAAAAA"
LOCAL cWord := "<cCod>"
LOCAL aPos := {}
LOCAL i

aPos := mypos( cString , cWord )
IF Len( aPos ) > 0
xBrowse( aPos )
ELSE
MsgStop( 'Word not found!' )
END

RETURN NIL

FUNCTION MYPOS( cString, cWord )
LOCAL aPos := {}
LOCAL i := 1

i := At( cWord, cString )
WHILE i > 0
Aadd( aPos, i )
i := At( cWord, cString, i+1 )
END

RETURN aPos

Posts: 332
Joined: Thu Nov 17, 2005 09:11 PM
Re: Searching string
Posted: Tue Sep 11, 2012 01:20 PM

Thanks for all sugestions!

Continue the discussion