lua string.find无法识别子字符串 [英] lua string.find not recognising substring

查看:64
本文介绍了lua string.find无法识别子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将LUA用作Minecraft Mod的一部分-我不确定会对事情有多大影响-请参阅openComputers.

I am using LUA as part of a minecraft mod - I am not sure how much that affects things - see openComputers.

代码尝试将给定的字符串与通过迭代一系列包含名称的项的插槽获得的字符串相匹配.

The code attempts to match a given string to one obtained by iterating over a series of slots containing items which have names.

重要的代码块是:

    term.write("enter name")
    name=term.read()

返回一个字符串,例如森林"不带引号.我还检查了数据类型.

returns a string e.g. "Forest" without the quotation marks. I have also checked the datatype.

然后,代码将解析清单中的物品,以查找名称中带有森林"的物品.其价值的第一行在某种程度上是特定于游戏的,但输出应使其无关紧要.

Then later the code parses an inventory of items looking for one with "Forest" in its name. The first line for what it's worth is somewhat specific to the game but the output should render that unimportant.

    item = storage.getStackInSlot(sides.top,i)

这会返回一堆东西,但是会用:

this returns a bunch of stuff but is then refined with:

    item_name = item.label

返回"Bee Sample-Species:Forest"(我也检查过)的字符串肯定是一个字符串.

which returns "Bee Sample - Species: Forest" which I have also checked is definitely a string.

然后,代码为匹配/查找应返回!= nil的行返回nil.我什至在string.find操作之前打印了两个字符串.

The code then returns nil for the line where the match/find should return something != nil. I even print the two strings before the string.find operation.

    match = string.match(item_name, name, 1, true)
    print(match)

返回零.

我不知道为什么这会返回false.我已经测试了匹配并在解释器中单独查找功能,例如:

I have no clue why this would return false. I have tested the match and find functionality separately in interpreter with stuff like:

    a=bic
    b=ambico
    string.find(b,a)

很好.

很抱歉,这很明显,非常感谢

Sorry if this is super obvious and many thanks

推荐答案

我找到了一个解决方案,使用带有ascii代码的string.gsub而不是字符本身可以解决此问题.对于遇到相同问题的人:

I have found a solution, Using string.gsub with the ascii code instead of the character itself solves the problem. For those who encounter the same problem:

newstring = string.gsub(string, "[\10]", "")

这将删除换行符,并允许从此开始进行字符串匹配.

This removes the line feed and allows string matching henceforth.

这篇关于lua string.find无法识别子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆