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.
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.
#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 }MarcoBoschi wrote:#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
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
#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 aPosNumAt()
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.
You can also adapt below code:
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
Thanks for all sugestions!