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
相关推荐
喜欢的名字被抢了3 小时前
MySQL基础入门:从零理解数据库与SQL
数据库·mysql·教程
西门吹-禅4 小时前
java springboot N+1问题
java·开发语言·spring boot
儒雅的大叔5 小时前
MySQL数据库InnoDB数据恢复工具使用总结
数据库·mysql·adb
skywalk81635 小时前
设计并实现段言的 C FFI 绑定机制 @Trae
c语言·开发语言·python·编程
IT笔记6 小时前
【Rust】Rust Match 模式匹配详解
java·开发语言·rust
2zcode6 小时前
免费开源项目文档:基于MATLAB卷积神经网络的口罩佩戴检测系统
开发语言·matlab·cnn
逝水无殇7 小时前
C# 运算符重载详解
开发语言·后端·c#
TPBoreas7 小时前
配置信息防泄露方案:.env 环境隔离详解(dotenv-java)
java·开发语言
敲代码的嘎仔7 小时前
实习日志day6--实习日志day6--title命名规范化&businessType纠正&补充缺失的@Log注解&报警与通信模块补充&产出阶段总结文档
java·开发语言·人工智能·git·python·实习·大二
江华森8 小时前
Python 实现高德地图找房(一):环境搭建与数据爬虫
开发语言·爬虫·python