The numeric ocurrence of the token inside string
The numeric ocurrence of the token inside string
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,
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,
Last edited by avista on Thu Feb 16, 2012 7:47 am, edited 2 times in total.
-
- Posts: 366
- Joined: Wed Aug 30, 2006 5:25 pm
- Location: Uruguay
Re: The numeric ocurrence of the token inside string
HI;
I found 3 way to do it. Both with Nanforum toolkit NF.lib or hbnf.lib from Harbour
1 -
FT_NOOCCUR()
Find the number of times one string occurs in another
------------------------------------------------------------------------------
Syntax
FT_NOOCCUR( <cCheckFor>, <cCheckIn> ;
[, <lIgnoreCase> ] ) -> <nOccurrences>
Arguments
<cCheckFor> is the string to search for
<cCheckIn> is the string to search
<lIgnoreCase> is a boolean variable to force case sensitivity
(optional, defaults to .F.).
Returns
The number of times <cCheckFor> appears in <cCheckIn>
Description
This function finds the number of times a string occurs in a
second string.
Examples
// Find the number of times "the" appears in cMemoString, case
// insensitive
nNoOfOccurrences := FT_NOOCCUR( "the", cMemoString )
// Find the number of times "the" appears in cMemoString, case
// sensitive
nNoOfOccurrences := FT_NOOCCUR( "the", cMemoString, TRUE )
2 -
MsgInfo( FT_AT2( "sample", "this is the sample text" ) ) RETURN 13, the first character of Sample
3 -
FT_FINDITH()
Find the "ith" occurrence of a substring within a string
------------------------------------------------------------------------------
Syntax
FT_FINDITH( <cCheckFor>, <cCheckIn>, <nWhichOccurrence> ;
[, <lIgnoreCase> ] ) -> <nStringPosition>
Arguments
<cCheckFor> is the string to search for.
<cCheckIn> is the string to search.
<nWhichOccurrence> is the number of the occurrence to find.
<lIgnoreCase> is a logical indicating if the search is to be case
sensitive. The default is no case sensitivity (.F.).
Returns
The position in the string cCheckIn of the ith occurrence of cCheckFor.
Description
This function finds the position in a string of the "ith" time another
string appears in it.
Examples
// Find the Position in cMemoString of
// the 10th Occurrence of "the", case
// insensitive
nNextPosition := FT_FINDITH("the", cMemoString, 10)
Best regads
Ruben Fernandez
I found 3 way to do it. Both with Nanforum toolkit NF.lib or hbnf.lib from Harbour
1 -
FT_NOOCCUR()
Find the number of times one string occurs in another
------------------------------------------------------------------------------
Syntax
FT_NOOCCUR( <cCheckFor>, <cCheckIn> ;
[, <lIgnoreCase> ] ) -> <nOccurrences>
Arguments
<cCheckFor> is the string to search for
<cCheckIn> is the string to search
<lIgnoreCase> is a boolean variable to force case sensitivity
(optional, defaults to .F.).
Returns
The number of times <cCheckFor> appears in <cCheckIn>
Description
This function finds the number of times a string occurs in a
second string.
Examples
// Find the number of times "the" appears in cMemoString, case
// insensitive
nNoOfOccurrences := FT_NOOCCUR( "the", cMemoString )
// Find the number of times "the" appears in cMemoString, case
// sensitive
nNoOfOccurrences := FT_NOOCCUR( "the", cMemoString, TRUE )
2 -
MsgInfo( FT_AT2( "sample", "this is the sample text" ) ) RETURN 13, the first character of Sample
3 -
FT_FINDITH()
Find the "ith" occurrence of a substring within a string
------------------------------------------------------------------------------
Syntax
FT_FINDITH( <cCheckFor>, <cCheckIn>, <nWhichOccurrence> ;
[, <lIgnoreCase> ] ) -> <nStringPosition>
Arguments
<cCheckFor> is the string to search for.
<cCheckIn> is the string to search.
<nWhichOccurrence> is the number of the occurrence to find.
<lIgnoreCase> is a logical indicating if the search is to be case
sensitive. The default is no case sensitivity (.F.).
Returns
The position in the string cCheckIn of the ith occurrence of cCheckFor.
Description
This function finds the position in a string of the "ith" time another
string appears in it.
Examples
// Find the Position in cMemoString of
// the 10th Occurrence of "the", case
// insensitive
nNextPosition := FT_FINDITH("the", cMemoString, 10)
Best regads
Ruben Fernandez
Gracias y Saludos
Ruben Fernandez - Uruguay
FWH 11.06, Harbour, Borland 5.82
Ruben Fernandez - Uruguay
FWH 11.06, Harbour, Borland 5.82
Re: The numeric ocurrence of the token inside string
Seems good but there are errors
Unresolved external '_HB_FUN_FT_FINDITH'
Need i some other lib ?
Regards
p.s
Isnt it some function simular to StrToken()
Unresolved external '_HB_FUN_FT_FINDITH'
Need i some other lib ?
Regards
p.s
Isnt it some function simular to StrToken()
- Antonio Linares
- Site Admin
- Posts: 37481
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Contact:
Re: The numeric ocurrence of the token inside string
Try this:
MsgInfo( AScan( hb_ATokens( cLine, " " ), { | cToken | cToken == "sample" } ) )
MsgInfo( AScan( hb_ATokens( cLine, " " ), { | cToken | cToken == "sample" } ) )
Re: The numeric ocurrence of the token inside string
Going good
Thanks Antonio
Regards,
Thanks Antonio
Regards,
Re: The numeric ocurrence of the token inside string
Hi Antonio,
function hb_ATokens dont work good.
It return good value just in case if there is ONLY one space between all tokens
Example:
MsgInfo( AScan( hb_ATokens( cText1, " " ), { | cToken | cToken == "sample" } ) ) -> Return 4
MsgInfo( AScan( hb_ATokens( cText2, " " ), { | cToken | cToken == "sample" } ) ) -> Retunr 7
Any other solution please ?
Best regards.
function hb_ATokens dont work good.
It return good value just in case if there is ONLY one space between all tokens
Example:
Code: Select all
cText1 := "this is a sample text"
cText2 := "this is a sample text"
MsgInfo( AScan( hb_ATokens( cText2, " " ), { | cToken | cToken == "sample" } ) ) -> Retunr 7
Any other solution please ?
Best regards.
Re: To Antonio: The numeric ocurrence of the token inside string
Hi,
Code: Select all
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" } ) )
Re: To Antonio: The numeric ocurrence of the token inside string
Hi,
You can try by first replacing all multiples spaces to just one space by using the function "atrepl()" inside a for-next instruction:
until you have just one space among each word in cString variable, after that, you can apply Antonio's suggestion.
Regards,
George
You can try by first replacing all multiples spaces to just one space by using the function "atrepl()" inside a for-next instruction:
Code: Select all
atrepl(" ", cString, "" )
Regards,
George
Re: To Antonio: The numeric ocurrence of the token inside string
Thanks to all for sugestions
In this time this is my opinion ...
But isnt it simular function in fivewin ?
Best regards,
In this time this is my opinion ...
Code: Select all
#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
Best regards,
- Antonio Linares
- Site Admin
- Posts: 37481
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Contact:
Re: To Antonio: The numeric ocurrence of the token inside string
Try this:
local cText := "This is another test"
MsgInfo( At( StrToken( cText, 3 ), cText ) )
local cText := "This is another test"
MsgInfo( At( StrToken( cText, 3 ), cText ) )
Re: To Antonio: The numeric ocurrence of the token inside string
Hi,
I dont understand your sample Antonio,
In your sample i need to know on which position is token "another"
I dont know that it is on position 3 (if you think that)
hmpaquito sample working good
function i created working good too and it is smaller faster and easyer .
I am not only sure is it simular function if fivewin ?
Best regards,
I dont understand your sample Antonio,
In your sample i need to know on which position is token "another"
I dont know that it is on position 3 (if you think that)
hmpaquito sample working good
function i created working good too and it is smaller faster and easyer .
Code: Select all
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 ) )
Best regards,
-
- Posts: 824
- Joined: Thu Oct 13, 2005 7:39 am
- Location: Germany
Re: The numeric ocurrence of the token inside string
Avista,
here is a working sampleavista wrote:MsgInfo( AScan( hb_ATokens( cText1, " " ), { | cToken | cToken == "sample" } ) ) -> Return 4Code: Select all
cText1 := "this is a sample text" cText2 := "this is a sample text"
MsgInfo( AScan( hb_ATokens( cText2, " " ), { | cToken | cToken == "sample" } ) ) -> Retunr 7
Code: Select all
#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)
kind regards
Stefan
Stefan
- Antonio Linares
- Site Admin
- Posts: 37481
- Joined: Thu Oct 06, 2005 5:47 pm
- Location: Spain
- Contact:
Re: The numeric ocurrence of the token inside string
MsgInfo( AScan( hb_ATokens( StrTran( cLine, " ", " " ), " " ), { | cToken | cToken == "sample" } ) )
StrTran( cLine, " ", " " ) will leave just one space between each token
notice first " " have two spaces and second " " just one.
StrTran( cLine, " ", " " ) will leave just one space between each token
notice first " " have two spaces and second " " just one.