Detecting where a string is using strfind?
#1

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

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

hello there.

"hel" would be the first 3 characters of the message.
Reply
#4

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

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

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

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

You're welcome!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)