string.find problem

I have a problem that I don’t figure out.

at the first part I have this


if (string.find(arguments, "vortigaunt")) then

when I type in the command and have vortigaunt as arguments it works.

then I have this


elseif (string.find(arguments, "enslaved vortigaunt")) then

when I type “Enslaved Vortigaunt” it takes Vortigaunt and runs the first line

I made an experiment and just wrote this


elseif (string.find(arguments, "enslaved")) then

and both worked but I want to let it work like this



elseif (string.find(arguments, "enslaved vortigaunt")) then

how do I fix this?

Consider your logic.

Your first statement, “if vortigaunt”, checks if the string contains the word “vortigaunt”. This is going to be true if you have the word anywhere in the command - and since it’s first, all of your “elseif” statements are skipped over.

What you need to do is ensure that your most specific commands are first in the “if-else” list - in other words, you want something like this:



if (string.find(arguments, "enslaved vortigaunt")) then

	--Do a thing

elseif (string.find(arguments, "vortigaunt")) then

	--Do the other thing

end


Also, he said he typed “Enslaved Vor”. With capitals. That would also be a problem since it is searching for lowercase.

You are both wrong.

string.find finds the string for anywhere like alfsjalfjalfjslajflvortigauntasfjslfajslfjasfkj this would work also

Then I typed in Vortigaunt and it worked too that is the only reason why I used string.find but the problem is I can’t actually use it correctly now so I still please for help.

No, they’re both correct. Make your arguments lowercase before testing them with string.find, then, search for macro strings first; your order would be “enslaved vortigaunt” then “vortigaunt”.

Yep, we are both wrong. You are right. So sorry for our inferior knowledge.

SO SORRY. Maybe next time don’t even post a thread? If you are gonna call the help you get wrong then just don’t post. You obviously know what you are doing.

Use string.StartWith or string.match. if you need position of match result you can use string.find where second argument is match pattern, look more at wiki.
Examples:


string.StartWith(arguments,"enslaved") -- quicker than below

string.match(arguments,"^enslaved vortigaunt$")

But better to use simple functions:


arguments == "enslaved vortigaunt" or arguments == "vortigaunt" -- quicker than below

string.sub(arguments,0,8)=="enslaved" or string.sub(argumens,0,10)=="vortigaunt"

And not forget to use string.lower. Because strings can contain uppercase characters like VorTiguant not work without this function.
If you wanna make function with arguments look at string.Trim or builtin function: string.gmatch(arguments,"%w+")
Good luck!

So if I have string.find(“one two three”)

I put it really high and if I have

string.find(“one”) I put it down right?

You know actually, instead of doing it like that you should just make it so it check the whole string.

Check the string length to make sure it is a certain length and to make sure it is that string. Pretty much solves your problemo.

Like so,



stringinput = string.lower(stringinput)

if string.len(stringinput) == 10 and  stringinput == "vortigaunt" then
    blah blah blah
elseif string.len(stringinput) == 19 and stringinput == "enslaved_vortigaunt" then
    blah blah blah
end


*Ignore this, I thought you had a different problem.

What’s the point of checking the length of the string?

[editline]19th December 2016[/editline]

If the string is equal to “vortigaunt”, it’s going to be a length of 10. If the string is equal to “enslaved_vortigaunt”, it’s going to be a length of 19.

Because as so stated before, if someone types in “vortigaunt” and it has the check of “enslaved_vortigaunt” first. Then for all hell reason it runs first, shouldn’t be doing that but for whatever reason it is. So the string check makes sure it is long as it’s supposed to be to make sure it runs properly.

Edit*

I see he is using string.find. I don’t get why he is using that but he is.
So correction, stop using string.find. Just check the argument.

Like so,

instead of



string.find(arugements, "vortiguant")


do



if arguments == "vortiguant" then
end