SA-MP Forums Archive
How do I check if a string Equals a certain word? - 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: How do I check if a string Equals a certain word? (/showthread.php?tid=213666)



How do I check if a string Equals a certain word? - ViperSniper - 19.01.2011

pawn Код:
COMMAND:gender(playerid,params[])
{
    SetPVarString(playerid,"Gender",params);
    new strz[38];
    format(strz,sizeof(strz), "Your gender is now set to: %s.",params);
    SendClientMessage(playerid, blue, strz);
    return 1;
}
I want to check if 'params' doesn't equal Male or Female (and if possible ignoring the case, so fEmAle still equals Female).
and if it doesn't then stops the command and sends a Invalid gender message.
Well heres how I would do it in java..
Код:
if(!params.equalsIgnoreCase("male") || params.equalsIgnoreCase("Female"))
But I have no idea how pawn strings work, I just learned about the lengths being how many characters are in it +1 for the null character ending the string...

Thanks for reading.


Bonus question :
Is 12 characters long enough for an Accent?
And should it be >= 14 because of the null characters on strings?
Код:
COMMAND:accent(playerid,params[])
{
    if(strlen(params) >= 13)
    {
        SendClientMessage(playerid, blue, "[LENGTH ERROR] Accent cannot be over 12 characters long.");
        return 1;
    }
	SetPVarString(playerid,"Accent",params);
	new strz[44];
	format(strz,sizeof(strz), "Your accent is now set to: %s.",params);
	SendClientMessage(playerid, blue, strz);
	return 1;
}



Re: How do I check if a string Equals a certain word? - Macluawn - 19.01.2011

Can we answer the bonus question only after answering the main question?


Re: How do I check if a string Equals a certain word? - ViperSniper - 19.01.2011

Quote:
Originally Posted by Macluawn
Посмотреть сообщение
Can we answer the bonus question only after answering the main question?
Yeah, sorry.


Re: How do I check if a string Equals a certain word? - ViperSniper - 19.01.2011

I looked through Wiki and found strcmp, I'm about to test it to see if its correct for what I'm wanting..
Код:
COMMAND:gender(playerid,params[])
{
	if(!strcmp(params, "female", true, 6) || !strcmp(params, "male", true, 4))
	{
		SetPVarString(playerid,"Gender",params);
		new strz[38];
		format(strz,sizeof(strz), "Your gender is now set to: %s.",params);
		SendClientMessage(playerid, blue, strz);
	} else {
        SendClientMessage(playerid, blue, "INVALID GENDER, Male or Female.");
	}
	return 1;
}
Correct or Incorrect..

Edit: Oops sorry..

Edit (2):
Ok, that kind of works, but i can do /gender femaleanything /gender maleanything


Re: How do I check if a string Equals a certain word? - WillyP - 19.01.2011

pawn Код:
COMMAND:gender(playerid,params[])
{
    if(!sscanf(params)) return SendClientMessage(playerid,-1,"You need to select a gender!");
    new strz[38];
    format(strz,sizeof(strz), "Your gender is now set to: %s.",params);
    SetPVarString(playerid,"Gender",params);
    SendClientMessage(playerid, blue, strz);
    return 1;
}



Re: How do I check if a string Equals a certain word? - ViperSniper - 19.01.2011

Quote:
Originally Posted by [FU]Victious
Посмотреть сообщение
pawn Код:
COMMAND:gender(playerid,params[])
{
    if(!sscanf(params)) return SendClientMessage(playerid,-1,"You need to select a gender!");
    new strz[38];
    format(strz,sizeof(strz), "Your gender is now set to: %s.",params);
    SetPVarString(playerid,"Gender",params);
    SendClientMessage(playerid, blue, strz);
    return 1;
}
Hmm, By looking at that I'm guessing
if(!sscanf(params)) return SendClientMessage(playerid,-1,"You need to select a gender!");
Is checking if theres no value