How to check if a string contains spaces?
#1

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

Yes, use this:

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

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

Thanks!
Reply
#5

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

Reply
#6

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

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

Oops, hehe... It works fine now.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)