Author Topic: Finding letter or word from string? Possible?  (Read 3453 times)

Rocky

  • Full Member
  • ***
  • Posts: 105
  • Karma: +5/-3
    • View Profile
Finding letter or word from string? Possible?
« on: April 10, 2013, 07: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;
}
Look for lu server testers, Requirements are:
English good enough to communicate.
Good knowledge about gaming.

PM me those who are interested.

Thijn

  • Tester
  • Sr. Member
  • ****
  • Posts: 531
  • Karma: +27/-16
    • View Profile
Re: Finding letter or word from string? Possible?
« Reply #1 on: April 10, 2013, 04:34:41 pm »
You can do
Code: [Select]
foreach( char in InvalidNicks )
{
   if ( Nick.find(char) != null )
   {
      //contains illegal character
   }
}

Rocky

  • Full Member
  • ***
  • Posts: 105
  • Karma: +5/-3
    • View Profile
Re: Finding letter or word from string? Possible?
« Reply #2 on: April 10, 2013, 06: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
« Last Edit: April 11, 2013, 08:27:39 am by Rocky »
Look for lu server testers, Requirements are:
English good enough to communicate.
Good knowledge about gaming.

PM me those who are interested.

Juppi

  • Developer
  • Jr. Member
  • *****
  • Posts: 86
  • Karma: +3/-1
    • View Profile
    • Kuslahden alaste GTA:MP clan
Re: Finding letter or word from string? Possible?
« Reply #3 on: April 10, 2013, 07: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.

Rocky

  • Full Member
  • ***
  • Posts: 105
  • Karma: +5/-3
    • View Profile
Re: Finding letter or word from string? Possible?
« Reply #4 on: April 11, 2013, 08: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.
Look for lu server testers, Requirements are:
English good enough to communicate.
Good knowledge about gaming.

PM me those who are interested.

 

© Liberty Unleashed Team.