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
相关推荐
Nontee8 小时前
MySQL 插入冲突了怎么办?两种处理方式入门笔记
数据库·笔记·mysql
m0_547486669 小时前
《JavaScript核心原理 》全套PPT课件2026
开发语言·javascript·ecmascript
MC皮蛋侠客9 小时前
Tauri 2.x 系列(五):调用系统 API——官方插件、Rust crate 与原生能力
开发语言·后端·rust
devilnumber10 小时前
MySQL 性能优化・诗意化记忆
数据库·mysql·性能优化
布莱克60510 小时前
软中断和硬中断的区别
开发语言·操作系统
whcyhhh11 小时前
头歌实践教学平台:数据科学与大数据技术导论(十二)
大数据·开发语言·python·数据清洗
lzhdim11 小时前
13、JavaScript事件循环机制 - JavaScript学习系列文章
开发语言·前端·javascript·学习·ecmascript
zbyyd12 小时前
Linux 多线程:互斥锁 & 信号量
linux·c语言·开发语言·网络
wuyk55512 小时前
从零吃透Modbus通信|第3章:STM32 Modbus RTU主机
服务器·开发语言·网络·数据结构·stm32·单片机·嵌入式硬件
宠友信息12 小时前
社区类源码开发实践中的仿小红书系统技术要点分析
java·spring boot·redis·mysql·uni-app·vue·内容运营