Posts: 225
Threads: 45
Joined: Nov 2008
Reputation:
0
Hi,
How can I get the number of strings found in another string?
Example:
Full string: "This_is_a_test"
Search string: "_"
It should return 3 ("_" was found 3 times in the string).
Any solutions?
Posts: 6,129
Threads: 36
Joined: Jan 2009
strfind doesn't return how many times it's been found in a string, it returns the position of the string you've searched for.
Look at this as a string:
"John_Doe"
It would return 4.
"Jonas_Doe"
That would return 5.
pawn Код:
stock StringOccInString( stringtosearchfor[], string[] ) // Calgon.
{
new tmpcount;
for( new i = 0; i < strlen( string ); i++)
{
if( strfind( string[ i ], stringtosearchfor, true ) )
{
tmpcount++;
}
}
return tmpcount;
}
This will do what you want though.
Posts: 6,129
Threads: 36
Joined: Jan 2009
Quote:
Originally Posted by MadeMan
This is another option
pawn Код:
stock GetNumberOfChar(const src[], c) { new len = strlen(src); new count; for(new i=0; i<len; i++) { if(src[i] == c) { count++; } } return count; }
Usage:
pawn Код:
GetNumberOfChar("This_is_a_test", '_')
|
That's what I posted above, except you use 1 more variable than I use.
Quote:
Originally Posted by ¤Adas¤
lmao, that will not work.
|
Missed a bracket, fixed.