lua 判断字符串是否包含子字符串(点符号查找)

一、string.find 方法

lua代码

Lua 复制代码
function containsDot(str)  
    local pos = string.find(str, ".")  
    if pos then  
        return true  
    else  
        return false  
    end  
end  
  
-- 测试函数  
local testString1 = "hello.world"  
local testString2 = "helloworld"  
  
print(containsDot(testString1)) -- 输出: true  
print(containsDot(testString2)) -- 输出: true,  为什么呢?

二、注意事项

1、匹配点(.)会返回true

在正则表达式中,点(.)是一个特殊字符,它匹配除了换行符之外的任何单个字符。因此,我们使用 %. 来匹配实际的点字符(.),其中 % 是 Lua 中正则表达式中的转义字符。

Lua 复制代码
function containsDot(str)  
    local pos = string.find(str, "%.")  --使用 %. 来匹配实际的点字符
    if pos then  
        return true  
    else  
        return false  
    end  
end  
  
-- 测试函数  
local testString1 = "hello.world"  
local testString2 = "helloworld"  
  
print(containsDot(testString1)) -- 输出: true  
print(containsDot(testString2)) -- 输出: false
相关推荐
阿奇__几秒前
uniapp支付宝 H5 开发踩坑,hash模式下取参要规范!
开发语言·uni-app
LinuxGeek10242 分钟前
Kylin-Server-V11、openEuler-22.03和openEuler-24.03的MySQL 9.7.0版本正式发布
大数据·mysql·kylin
eggwyw3 分钟前
PHP搭建开发环境(Windows系统)
开发语言·windows·php
一行代码一行诗++21 分钟前
C语言中scanf详解
c语言·开发语言
凤山老林24 分钟前
26-Java this 关键字
java·开发语言
ZenosDoron30 分钟前
keil软件修改字体,Asm editor,和C/C++ editor的区别
c语言·开发语言·c++
山栀shanzhi1 小时前
C/C++之:构造函数为什么不能设置为虚函数?
开发语言·c++·面试
lsx2024061 小时前
.toggleClass() 方法详解
开发语言
yuan199971 小时前
C&CG(列与约束生成)算法,来解决“风光随机性”下的微网鲁棒配置问题
c语言·开发语言·算法
李白的天不白1 小时前
读到数据为undefind是的几种情况
开发语言·javascript·ecmascript