Author Topic: [SOLVED] Using strLen, and strStr in a function  (Read 1411 times)

How would I go about using strStr and strLen in a function?
« Last Edit: December 11, 2011, 11:26:49 AM by FFSO »

They are very versatile, so it depends on what you want to do with them. If you know how they work, it should only take a bit of thought to figure out.

strStr takes two arguments, and searches through the first argument, trying to find a copy of the second one. If it does, it returns the position (how many characters after the start, so it would be 0 if you used strStr("food!", "foo"), or 5 if you used strStr("blargablar", "abl")), or -1 if it can't find it.

Since it returns a number, you can use it anywhere you could use a number. In general, however, you would use it to see if a string contains a specific character or phrase (checking if it is <= 0 or < -1), or anywhere else that you care where/if a string contains a second string.


strLen takes a single argument, and returns how many characters it has.



Both functions do nothing on their own, but they return useful numbers. In general, you would store the result in a variable (%len = strLen("someText"), %pos = strStr(%param, "I"), or %len = strLen(%obj.name) would be valid examples), or use them directly in equations (if(strStr(%message, "kill") != -1), strLen(%var) + 42)

It really depends on what you want to do, and it's all logic. Keep in mind that coding is not voodoo, you don't just wave your hands and type some meaningless cryptic phrase and everything works, there is a logical reason for everything. Unfortunately, some people ask for "help" and really just want the mystical incantation that makes magical code happen. If you put some thought into it, you can outdo them all...


Edit: If you are doing simpler things, like storing data in a string, look into the word, field, and record functions. They let you work with a string as if it was split by spaces, tabs, or newlines.

It's rather hard to give the best advise possible when you don't know if someone knows about any specific functions or not, or what their skill level is...
« Last Edit: December 10, 2011, 10:30:24 PM by Uristqwerty »

so..
Code: [Select]
function Determine(%position, %pos2)
{
    %position = strStr(%position, %pos2);

    if(%position = "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")
    {
        if(%pos2 = "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")
        {
            echo(strStr(%position, %pos2));
        }
    }
}
Would that work?
« Last Edit: December 10, 2011, 10:56:21 PM by FFSO »

Are you just checking if its a letter?

I was trying to make it check if the word had any letter in the alphabet. Ill correct in the morning.

I was trying to make it check if the word had any letter in the alphabet. Ill correct in the morning.
Then you would do something like this:

Code: [Select]
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;
}
If the string has any letter in it then this will return true.

That's a pretty slow method to do it, something like this is better:
Code: [Select]
function stringHasLetter(%string)
{
if(%string $= "")
return false;

return stripChars(%string,"abcdefghijlmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") !$= %string;
}
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.

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

so...would it be
Code: [Select]
function stringHasLetter(%string, %string2)
{
    if(%string $= "" || %string2 $= "")
        return false;

    return stripChars(%string,"abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMONPQRSTUVWXYZ") !$= %string & %string2;
    echo(strStr(%string, %string2));
}
??? 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,
« Last Edit: December 11, 2011, 10:57:55 AM by FFSO »

Because you're returning before the echo.
Also this:
Code: [Select]
%string & %string2I'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: [Select]
%stripped = stripChars(%string,"abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMONPQRSTUVWXYZ");
%result = %stripped !$= %string && %stripped !$= %string2;

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

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

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

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

Yes greek you are correct, that is exactly what I was trying to do. It worked too.

ENDING CODE:
Code: [Select]
function stringHasLetter(%string, %letter)
{
    if(%string $= "" || %letter $= "")
        return false;

    echo(strStr(%string, %letter));
    return (strPos(%string,%letter) >= 0);
}