Liberty Unleashed

Scripting => Scripting Discussion => Topic started by: Rocky on April 10, 2013, 06:00:02 am

Title: Finding letter or word from string? Possible?
Post 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.

Code: [Select]
InvalidNicks <- [ "@", "!", "#", "$", "%", "^", "&", "*", "(", ")", "-", "`", "~", ".", "-", "+"];
Code: [Select]
  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;
}
Title: Re: Finding letter or word from string? Possible?
Post by: Thijn on April 10, 2013, 03:34:41 pm
You can do
Code: [Select]
foreach( char in InvalidNicks )
{
   if ( Nick.find(char) != null )
   {
      //contains illegal character
   }
}
Title: Re: Finding letter or word from string? Possible?
Post by: Rocky on April 10, 2013, 05:55:09 pm
You can do
Code: [Select]
foreach( char in InvalidNicks )
{
   if ( Nick.find(char) != null )
   {
      //contains illegal character
   }
}
Coded all that long and waste anyway thanks alot dude:D
Title: Re: Finding letter or word from string? Possible?
Post by: Juppi on April 10, 2013, 06:21:36 pm
If you only need to filter certain illegal characters this is probably the best method as it only compares single characters instead of strings:

Code: [Select]
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.
Title: Re: Finding letter or word from string? Possible?
Post by: Rocky on April 11, 2013, 07:28:36 am
If you only need to filter certain illegal characters this is probably the best method as it only compares single characters instead of strings:

Code: [Select]
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.