SA-MP Forums Archive
Detecting where a string is using 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: Detecting where a string is using strfind? (/showthread.php?tid=393464)



Detecting where a string is using strfind? - 2KY - 18.11.2012

pawn Code:
public OnPlayerText(playerid, text[])
{
    if( strfind( text, "~r~", true ) != -1 )
    {
        new
            cStr [ 128 ],
            plName [ MAX_PLAYER_NAME ];
           
        GetPlayerName( playerid, plName, MAX_PLAYER_NAME );
           
        strdel( text, 0, 3 );
        format( cStr, 128, ""#I_White"%s says: "#I_ARed"%s", plName, text );
        SendClientMessageToAll( -1, cStr );
        return false;
    }
    return true;
}
I've got that - and it works fine, but if I put "hi ~r~" it will still turn the text red, and delete the message, rather than the red tag. Basically, how do I check if "~r~" is the first 3 characters of the message?


Re: Detecting where a string is using strfind? - Faisal_khan - 18.11.2012

I don't get it with "the first 3 characters of the message". ??


Re: Detecting where a string is using strfind? - 2KY - 18.11.2012

hello there.

"hel" would be the first 3 characters of the message.


Re: Detecting where a string is using strfind? - [KHK]Khalid - 18.11.2012

pawn Code:
if(text[0] == '~' && (text[1] == 'r' || text[1] == 'R') && text[2] == '~')
{
    // First 3 chars are ~r~, do something...
}
I think this doesn't need explanation, does it?


Re: Detecting where a string is using strfind? - Faisal_khan - 18.11.2012

Ah.. I did something like this hope it works:
pawn Code:
public OnPlayerText(playerid, text[])
{
    if(( strfind( text, "~r~", true ) != -1 ) && !( strfind( text, "~r~", true, 3 ) != -1))
    {
        new
            cStr [ 128 ],
            plName [ MAX_PLAYER_NAME ];

        GetPlayerName( playerid, plName, MAX_PLAYER_NAME );

        strdel( text, 0, 3 );
        format( cStr, 128, ""#I_White"%s says: "#I_ARed"%s", plName, text );
        SendClientMessageToAll( -1, cStr );
        return false;
    }
    return true;
}



Re: Detecting where a string is using strfind? - 2KY - 18.11.2012

Quote:
Originally Posted by HellSphinX
View Post
pawn Code:
if(text[0] == '~' && (text[1] == 'r' || text[1] == 'R') && text[2] == '~')
{
    // First 3 chars are ~r~, do something...
}
I think this doesn't need explanation, does it?
Nope! Completely forgot about that method, lol. Thank you.


Re: Detecting where a string is using strfind? - Vince - 18.11.2012

strfind returns the location where the substring occurs in the base string. If the substring occurs right in the front of the base string, then the returned value will be 0.


Re: Detecting where a string is using strfind? - [KHK]Khalid - 18.11.2012

You're welcome!