Blockland Forums > Modification Help

[SOLVED] Using strLen, and strStr in a function

Pages: << < (2/3) > >>

Greek2me:


--- Quote from: FFSO on December 10, 2011, 11:18:25 PM ---I was trying to make it check if the word had any letter in the alphabet. Ill correct in the morning.

--- End quote ---
Then you would do something like this:


--- Code: ---function stringHasLetter(%string)
{
%alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
for(%i=0; %i < getWordCount(%alphabet); %i++)
{
if(strPos(%string,getWord(%alphabet,%i)) >= 0)
return 1;
}
return 0;
}
--- End code ---
If the string has any letter in it then this will return true.

Destiny/Zack0Wack0:

That's a pretty slow method to do it, something like this is better:

--- Code: ---function stringHasLetter(%string)
{
if(%string $= "")
return false;

return stripChars(%string,"abcdefghijlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") !$= %string;
}

--- End code ---
Also proof that it's slower:
Average time for 5x 100,000 iterations of my method: 3327 milliseconds.
Average time for 5x 100,000 iterations of your method: 4277 milliseconds.

FFSO:

There was supposed to be 2 strings since it is using strStr(%string, %string2);

so...would it be

--- Code: ---function stringHasLetter(%string, %string2)
{
    if(%string $= "" || %string2 $= "")
        return false;

    return stripChars(%string,"abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMONPQRSTUVWXYZ") !$= %string & %string2;
    echo(strStr(%string, %string2));
}

--- End code ---
??? I was just winging it but it looks correct.
EDIT: Doesn't echo but the function has no syntax errors, apparently echo(strStr(%string, %string2)); is not working,

Headcrab Zombie:

Because you're returning before the echo.
Also this:

--- Code: ---%string & %string2
--- End code ---
I'm not sure what you're trying to do here, but I don't think you're doing what you think you're doing. Using bitwise operators on strings will always return 0 if the string isn't/doesn't start with a number.

Are you maybe trying to do something like this?

--- Code: ---%stripped = stripChars(%string,"abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMONPQRSTUVWXYZ");
%result = %stripped !$= %string && %stripped !$= %string2;
--- End code ---


Greek2me:

I think I know what he wants now. He just wants to check if the string has a specific letter in it.


--- Code: ---function stringHasLetter(%string,%letter)
{
if(%string $= "" || %letter $= "")
return false;

return (strPos(%string,%letter) >= 0);
}
--- End code ---

So then stringHasLetter("blah blah blah","e"); would return false and stringHasLetter("blah blah blah","a"); would return true.

Pages: << < (2/3) > >>

Go to full version