Liberty Unleashed
Scripting => Scripting Discussion => Topic started by: Rocky on April 10, 2013, 06:00:02 am
-
I mean Finding a letter or word from a string. Is there a function already made in squirrel?
This is what i coded for making Invalid Kick.
InvalidNicks <- [ "@", "!", "#", "$", "%", "^", "&", "*", "(", ")", "-", "`", "~", ".", "-", "+"]; local Nick = player.Name, i = 1, b = 0;
foreach( char in InvalidNicks )
{
while(i < Nick.len()+1 )
{
if ( Nick.slice( b, i ) == char )
{
Message("[#FF00FF]Kicking:[ " + player.Name + " ] for:[ Invalid Nickname ]");
KickPlayer( player );
return 0;;
}
i++;
b++;
}
i = 1;
b = 0;
}
-
You can do
foreach( char in InvalidNicks )
{
if ( Nick.find(char) != null )
{
//contains illegal character
}
}
-
You can do
foreach( char in InvalidNicks )
{
if ( Nick.find(char) != null )
{
//contains illegal character
}
}
Coded all that long and waste anyway thanks alot dude:D
-
If you only need to filter certain illegal characters this is probably the best method as it only compares single characters instead of strings:
function IsValidNick( nick )
{
for ( local i = 0; i < nick.len(); i++ )
{
switch ( nick[i] )
{
case '@':
case '!':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '(':
case ')':
case '-':
case '`':
case '~':
case '.':
case '-':
case '+':
return false;
}
}
return true;
}
The code is untested but it should work fine. If you need to find whole words then you'd be better off using string.find like Thijn suggested.
-
If you only need to filter certain illegal characters this is probably the best method as it only compares single characters instead of strings:
function IsValidNick( nick )
{
for ( local i = 0; i < nick.len(); i++ )
{
switch ( nick[i] )
{
case '@':
case '!':
case '#':
case '$':
case '%':
case '^':
case '&':
case '*':
case '(':
case ')':
case '-':
case '`':
case '~':
case '.':
case '-':
case '+':
return false;
}
}
return true;
}
The code is untested but it should work fine. If you need to find whole words then you'd be better off using string.find like Thijn suggested.
Thanks Juppi.