Last Occurance strfind
#1

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

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

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;
    }
}
Reply
#4

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


Forum Jump:


Users browsing this thread: 1 Guest(s)