Hi all,
I need function or way how to find The numeric ocurrence of the token inside string
For example:
cString := "this is the sample text"
I need to know that token "sample" is on teh 4 position in the string
Best regards,
cText1 := "this is a sample text"
cText2 := "this is a sample text"
cText:= "this is the sample text"
cLine:= ""
nPos:= 1
nLen:= Len(cText)
DO WHILE nPos <= nLen
cCar:= SubStr(cText, nPos, 1)
IF cCar == Space(1)
DO WHILE nPos <= nLen .AND. SubStr(cText, nPos, 1) == Space(1)
nPos++
ENDDO
ELSE
nPos++
ENDIF
cLine+= cCar
ENDDO
MsgInfo( AScan( hb_ATokens( cLine, " " ), { | cToken | cToken == "sample" } ) )
atrepl(" ", cString, "" )
#include "FiveWin.ch"
//-------------
FUNCTION Main()
LOCAL cText1 := "this is a sample text"
LOCAL cText2 := "this is a sample text"
MsgInfo( TokenPosition( cText1, "sample" ) )
MsgInfo( TokenPosition( cText2, "sample" ) )
RETURN NIL
//---------------------------------------
FUNCTION TokenPosition( cString, cToken )
LOCAL aTokens := {}
LOCAL cLocalToken := ""
LOCAL i := 0
DO WHILE .t.
i++
cLocalToken := UPPER( StrToken( cString, i ) )
IF !EMPTY( cLocalToken )
AADD( aTokens, cLocalToken )
ELSE
EXIT
ENDIF
ENDDO
RETURN ASCAN( aTokens, UPPER( cToken ) )
//-------------------------------------
procedure AppSys // XBase++ requirement
RETURN
FUNCTION TokenPosition( cString, cToken )
LOCAL aTokens := {}
LOCAL cLocalToken := ""
LOCAL i := 0
DO WHILE .t.
i++
cLocalToken := UPPER( StrToken( cString, i ) )
IF !EMPTY( cLocalToken )
AADD( aTokens, cLocalToken )
ELSE
EXIT
ENDIF
ENDDO
RETURN ASCAN( aTokens, UPPER( cToken ) )
avista wrote:
- Code: Select all Expand view
cText1 := "this is a sample text"
cText2 := "this is a sample text"
MsgInfo( AScan( hb_ATokens( cText1, " " ), { | cToken | cToken == "sample" } ) ) -> Return 4
MsgInfo( AScan( hb_ATokens( cText2, " " ), { | cToken | cToken == "sample" } ) ) -> Retunr 7
#include "Fivewin.ch"
function main ()
local cText1 := "this is a sample text"
local cText2 := "this is a sample text text sample"
cText2 := CharOne (" ", cText2) // remove all duplicate spaces between the words
MsgInfo( AScan( hb_ATokens( cText1, " " ), { | cToken | cToken == "sample" } ), cText1 )
MsgInfo( AScan( hb_ATokens( cText2, " " ), { | cToken | cToken == "sample" } ), cText2 )
? cText1, cText2
RETURN (0)
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 90 guests