Number of strings in a string?
#1

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?
Reply
#2

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.
Reply
#3

lmao, that will not work.
Reply
#4

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", '_')
Reply
#5

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.
Reply
#6

Ok, thx.
I only have to search for one character.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)