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
相关推荐
SomeB1oody几秒前
【RustyML入门】3.7. 循环层
开发语言·后端·机器学习·rust·教程
言乐63 小时前
Python游戏水平测试辅助系统
开发语言·python·游戏·django·pygame
WWJA王文举10 小时前
I²C通信完整流程详解:START、地址、ACK、数据、Repeated START和STOP一次讲透
c语言·开发语言
zhanghaha131411 小时前
Python进阶教程:6_JSON 数据解析 —— 新手完全指南
开发语言·python·json
qq_4480111612 小时前
C语言中的动态内存分配
c语言·开发语言·php
一只fish12 小时前
优化器架构对比:Oracle vs PostgreSQL vs MySQL
mysql·postgresql·oracle
汉字萌萌哒13 小时前
2024CSP-J入门级C++真题详解
开发语言·c++
arbboter13 小时前
【网络工具】NetProxy网络代理用户手册
开发语言·网络·c#·网络代理·网络异常·代理工具
汉字萌萌哒14 小时前
2019CCF-CSP入门级C++试题解析
java·开发语言·c++
叠层归一研究院14 小时前
AGI 系统(十一):二阶网络 — 二阶递归 × 多主体 (元符号网络)
开发语言·人工智能·算法·php·agi