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
相关推荐
月光水岸New2 小时前
Ubuntu 中建的mysql数据库使用Navicat for MySQL连接不上
数据库·mysql·ubuntu
软件黑马王子2 小时前
C#初级教程(4)——流程控制:从基础到实践
开发语言·c#
我爱松子鱼2 小时前
mysql之规则优化器RBO
数据库·mysql
闲猫2 小时前
go orm GORM
开发语言·后端·golang
李白同学3 小时前
【C语言】结构体内存对齐问题
c语言·开发语言
人间打气筒(Ada)4 小时前
MySQL主从架构
服务器·数据库·mysql
和道一文字yyds4 小时前
MySQL 中的索引数量是否越多越好?为什么?如何使用 MySQL 的 EXPLAIN 语句进行查询分析?MySQL 中如何进行 SQL 调优?
数据库·sql·mysql
黑子哥呢?4 小时前
安装Bash completion解决tab不能补全问题
开发语言·bash
青龙小码农5 小时前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿5 小时前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法