Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I check strings equality in lua?

Just a straight forward beginner question, I am coding Lua stuff for Garrys Mod, learning by reading wiki and other codings.

if (self.Owner:SteamID( ) == "STEAM_0:1:44037488" ) then 

the above is the code I want to use, to check to see if the STEAM ID (which I believe is a string) is equal to my exact string.

Is this viable? Or is there another way I should do it?

like image 639
Howard Sun Avatar asked Dec 24 '14 07:12

Howard Sun


People also ask

What does == Do in Lua?

The operator == tests for equality; the operator ~= is the negation of equality. We can apply both operators to any two values. If the values have different types, Lua considers them different values. Otherwise, Lua compares them according to their types.

What is the correct way of checking string equality?

You can check the equality of two Strings in Java using the equals() method. This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

How do you check if something is a string in Lua?

To check wether a value is a string use type(value) == "string" . To check wether a value can be convertet do a number use tonumber(value) . It will either return a number or nil.


1 Answers

This should work exactly as you expect it to. In lua '==' for string will return true if contents of the strings are equal.

As it was pointed out in the comments, lua strings are interned, which means that any two strings that have the same value are actually the same string.

like image 120
lisu Avatar answered Sep 18 '22 08:09

lisu