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
相关推荐
Yeats_Liao36 分钟前
Go Web 编程快速入门 01 - 环境准备与第一个 Web 应用
开发语言·前端·golang
卓码软件测评36 分钟前
第三方CMA软件测试机构:页面JavaScript动态渲染生成内容对网站SEO的影响
开发语言·前端·javascript·ecmascript
*才华有限公司*41 分钟前
《爬虫进阶之路:从模拟浏览器到破解动态加载的实战指南》
开发语言·python
Liu1bo43 分钟前
【MySQL】表的约束
linux·数据库·mysql
胖胖的战士1 小时前
Mysql 数据库迁移
数据库·mysql
敲代码的嘎仔1 小时前
JavaWeb零基础学习Day4——Maven
java·开发语言·学习·算法·maven·javaweb·学习方法
星光一影1 小时前
大型酒店管理系统源码(多酒店版)
mysql·php
W.Buffer1 小时前
设计模式-工厂模式:解耦对象创建的设计艺术
java·开发语言·设计模式
czhc11400756631 小时前
LINUX1012 mysql GLIBC安装
数据库·mysql
小草儿7991 小时前
gbase8s之.net8连接8s之mysql模式(windows)demo
windows·mysql·.net