Searching string

Searching string

Postby Wanderson » 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.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Searching string

Postby MarcoBoschi » Wed Sep 05, 2012 1:06 pm

Code: Select all  Expand view

#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
User avatar
MarcoBoschi
 
Posts: 1016
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: Searching string

Postby Wanderson » Wed Sep 05, 2012 1:39 pm

MarcoBoschi wrote:
Code: Select all  Expand view

#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.
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm

Re: Searching string

Postby MarcoBoschi » Wed Sep 05, 2012 3: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
User avatar
MarcoBoschi
 
Posts: 1016
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: Searching string

Postby MarcoBoschi » Wed Sep 05, 2012 3:38 pm

Code: Select all  Expand view
#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
 
User avatar
MarcoBoschi
 
Posts: 1016
Joined: Thu Nov 17, 2005 11:08 am
Location: Padova - Italy

Re: Searching string

Postby StefanHaupt » Thu Sep 06, 2012 7: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
StefanHaupt
 
Posts: 824
Joined: Thu Oct 13, 2005 7:39 am
Location: Germany

Re: Searching string

Postby ryugarai27 » Mon Sep 10, 2012 1: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
User avatar
ryugarai27
 
Posts: 65
Joined: Fri Feb 13, 2009 12:03 pm
Location: Manila, Philippines

Re: Searching string

Postby Wanderson » Tue Sep 11, 2012 1:20 pm

Thanks for all sugestions!
Wanderson
 
Posts: 332
Joined: Thu Nov 17, 2005 9:11 pm


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 18 guests