lua 字符串相等判断
时间: 2023-08-15 09:13:09 浏览: 263
在 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`。
lua 脚本字符串对比
在 Lua 中,可以使用运算符 == 来对比两个字符串是否相等。例如:
```lua
local str1 = "hello"
local str2 = "world"
if str1 == str2 then
print("str1 and str2 are equal")
else
print("str1 and str2 are not equal")
end
```
如果希望忽略大小写进行字符串对比,可以使用 string 库中的函数 string.lower() 将字符串转换为小写后再进行对比。例如:
```lua
local str1 = "Hello"
local str2 = "hello"
if string.lower(str1) == string.lower(str2) then
print("str1 and str2 are equal (ignoring case)")
else
print("str1 and str2 are not equal (ignoring case)")
end
```
除了使用 == 运算符进行对比外,还可以使用 string 库中的其他函数进行字符串操作和对比,例如 string.find()、string.match() 等。