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
相关推荐
Byron Loong10 分钟前
【c++】为什么有了dll和.h,还需要包含lib
java·开发语言·c++
独隅25 分钟前
CodeX + Visual Studio Code 联动的全面指南
开发语言·php
坚果派·白晓明28 分钟前
【鸿蒙PC三方库移植适配框架解读系列】第一篇:Lycium C/C++ 三方库适配 — 概述与环境配置
c语言·开发语言·c++·harmonyos·开源鸿蒙·三方库·c/c++三方库
爱吃小白兔的猫1 小时前
LPA算法详解:一种近线性时间的图社区发现方法
开发语言·php
香蕉鼠片2 小时前
算法过程中不会的
开发语言·c++
阿旭超级学得完2 小时前
C++11包装器(function和bind)
java·开发语言·c++·算法·哈希算法·散列表
shizhan_cloud2 小时前
MySQL 索引优化 + 慢查询日志
数据库·mysql
輕華2 小时前
uv工具详解——Python包与项目管理器完全指南
开发语言·python·uv
Drache_long2 小时前
MySQL数据库(故障排除)
数据库·mysql