SA-MP Forums Archive
How to check if a string contains spaces? - 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: How to check if a string contains spaces? (/showthread.php?tid=315752)



How to check if a string contains spaces? - Superthijs - 04.02.2012

Hi, I've made a function to change a player's name, but when I use a space in the name,
the server crashes.
Is there any way to check if a string contains one or more spaces?


Re: How to check if a string contains spaces? - Universal - 04.02.2012

Yes, use this:

pawn Code:
strfind(string, " ")
This will return true if it contains spaces, false if not.


Re: How to check if a string contains spaces? - [XST]O_x - 04.02.2012

pawn Code:
new string[length];
for(new i = 0; i < strlen(string); i++)
{
    if(string[i] == ' ') string[i] = '_';
}
Will replace any space with an underscore.


Re: How to check if a string contains spaces? - Superthijs - 04.02.2012

Thanks!


Re: How to check if a string contains spaces? - Superthijs - 04.02.2012

But it gives a warning...

Code:
if(strfind(NewName, " ") == true) return SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}/SETNAME{FFFF00}: Spaces are not allowed in a name.");
Warning:

Quote:

C:\Documents and Settings\Superthijs\Mijn documenten\SA-MP Windows Server\filterscripts\Commands.pwn(173) : warning 213: tag mismatch




Re: How to check if a string contains spaces? - AndreT - 04.02.2012

Quote:
Originally Posted by Universal
View Post
Yes, use this:

pawn Code:
strfind(string, " ")
This will return true if it contains spaces, false if not.
Wrong, wrong, wrong. Read this before posting.

To see if the newName contains a space, use this:
pawn Code:
if(strfind(newName, " ") != -1)
{
    // yes, it does
}



Re: How to check if a string contains spaces? - [XST]O_x - 04.02.2012

I don't remember strfind returning a boolean...
pawn Code:
if(strfind(NewName, " ") != -1) return SendClientMessage(/* */);



Re: How to check if a string contains spaces? - Superthijs - 04.02.2012

Oops, hehe... It works fine now.