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
相关推荐
Nebula_g2 分钟前
JavaSE项目实践:红包雨-线程池/线程
java·开发语言
外收内放2 分钟前
Python基础语法练习题(56优化版补充)
开发语言·python
小羊没烦恼!3 分钟前
jQuery1.5的改进细节
java·服务器·开发语言·前端·c#
程序员老陆34 分钟前
C++ 未定义行为全景:对象模型、优化边界与工程防御
开发语言·c++·程序设计
嵌入式er1 小时前
影石系统组 一二面OC面经
java·开发语言
徐小黑ACG1 小时前
Golang 基础06 接口interface
开发语言·后端·golang
半杯咖啡半行码1 小时前
Qt 开发基础:版本演进、常用工具与多平台安装
开发语言·qt
下页、再停留1 小时前
【C#桌面客户端系列学习-8】批量导出数据为csv格式
开发语言·c#·visual studio
菜鸟~noob2331 小时前
【电子战】 第26篇:大气衰减——氧气与水汽吸收【含matlab代码】
开发语言·matlab
菜鸟~noob2331 小时前
【电子战】第25篇:自由空间、双射线与菲涅尔区【含matlab代码】
开发语言·算法·matlab