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
相关推荐
p***h6432 小时前
JavaScript在Node.js中的异步编程
开发语言·javascript·node.js
散峰而望2 小时前
C++数组(二)(算法竞赛)
开发语言·c++·算法·github
Porunarufu2 小时前
Java·关于List
java·开发语言
子不语1803 小时前
Python——函数
开发语言·python
ndjnddjxn3 小时前
Rust学习
开发语言·学习·rust
Y***K4343 小时前
MySQL网站
数据库·mysql
月光技术杂谈3 小时前
实战:C驱动框架嵌入Rust模块的互操作机制与完整流程
c语言·开发语言·rust·ffi·跨语言·bindgen·互操作
t198751283 小时前
基于MATLAB的指纹识别系统完整实现
开发语言·matlab
菜鸟‍3 小时前
【后端学习】MySQL数据库
数据库·后端·学习·mysql
笑非不退3 小时前
C# c++ 实现程序开机自启动
开发语言·c++·c#