SA-MP Forums Archive
Last Occurance strfind - 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)
+--- Thread: Last Occurance strfind (/showthread.php?tid=604625)



Last Occurance strfind - AdrianGabriel96 - 07.04.2016

Hello. Any idea how to find last occurence of a substring in a string. Because strfind gives me the first occurence. I don't know maybe a while(lastfound!=-1) lastfound=strfind(bla bla b,lastfound+1); and use offset of strfind... maybe you can help me. Thx.


Re: Last Occurance strfind - DRIFT_HUNTER - 08.04.2016

With native samp strfind - you cant.
Workaounds?

-Reverse strings then search them.
-Create custom strfind in pawn or plugin
-Your way with while.

Код:
stock strfindLast(str1[], str2[], bool:ignorecase = false)
{
	new found = strfind(str1, str2, ignorecase);
	new foundTmp;
	if( found != -1 )
	{
	    while((foundTmp = strfind(str1, str2, ignorecase, found+1)) != -1)
		{
		    found=foundTmp;
			printf("Found again...");
		}
		return found;
	}
	else return -1;
}
Didnt test it but it should work...


Re: Last Occurance strfind - Gammix - 08.04.2016

In case of characters only you can simply do this:

pawn Код:
for (new i = strlen(string); i >= 0; i--)
{
    if (string[i] = '.')
    {
        // Your code for index "i"
        break;
    }
}



Re: Last Occurance strfind - Vince - 08.04.2016

strfind has an offset position from which it starts searching. Thus, you can loop over the string back to front and use the current index as the position to start searching from. Something like:
PHP код:
findlast(string[], substring[])
{
    new 
length strlen(substring);
    for(new 
strlen(string) - length0-= length)
    {
        new 
result strfind(stringsubstringtruei);
        if(
result != -1)
            return 
result;
    }
    
    return -
1;

Not tested.