lua判断字符串是否相同
时间: 2023-10-31 09:48:35 浏览: 139
在Lua中可以使用“==”运算符来判断两个字符串是否相同。例如:
local str1 = "hello"
local str2 = "world"
local str3 = "hello"
if str1 == str2 then
print("str1 与 str2 不相同")
elseif str1 == str3 then
print("str1 与 str3 相同")
end
输出结果为:str1 与 str3 相同
相关问题
lua 字符串相等判断
在 Lua 中,可以使用 `==` 运算符来判断两个字符串是否相等。例如:
```lua
local str1 = "Hello"
local str2 = "Hello"
if str1 == str2 then
print("Strings are equal")
else
print("Strings are not equal")
end
```
运行以上代码,输出结果将是 "Strings are equal",表示两个字符串相等。
使用lua写一个判断字符串相等的工具函数
下面是一个使用 Lua 编写判断字符串相等的工具函数:
```lua
function string_eq(str1, str2)
return str1 == str2
end
```
使用方法:
```lua
str1 = "hello"
str2 = "world"
if string_eq(str1, str2) then
print("str1 and str2 are equal")
else
print("str1 and str2 are not equal")
end
```
这个函数接收两个字符串作为参数,使用 `==` 运算符对它们进行比较,如果两个字符串相等则返回 `true`,否则返回 `false`。