SA-MP Forums Archive
Number of strings in a string? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Number of strings in a string? (/showthread.php?tid=125776)



Number of strings in a string? - Programie - 05.02.2010

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?


Re: Number of strings in a string? - Calgon - 05.02.2010

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.


Re: Number of strings in a string? - ¤Adas¤ - 05.02.2010

lmao, that will not work.


Re: Number of strings in a string? - MadeMan - 05.02.2010

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



Re: Number of strings in a string? - Calgon - 05.02.2010

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.


Re: Number of strings in a string? - Programie - 05.02.2010

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