1
我想提取从HTTP请求收到的POST参数,该请求发送给我的NodeMCU。我怎样才能做到这一点?我正在考虑C#中的以下代码。如何在Lua中实现这个?如何从Lua中的字符串中获取特定文本?
我在C#代码:
// Response = "<action>Play</action><speed>1</speed><blah>lol</blah>"
// ValuetoSearch = "action"
public static string GetInformationFromResponse(string Response, string ValueToSearch, bool RemoveHtmlCharacters = true) {
string returnValue = "";
if (RemoveHtmlCharacters) {
Response = Response.Replace("<" + ValueToSearch + ">", ValueToSearch);
Response = Response.Replace("</" + ValueToSearch + ">", ValueToSearch);
Response = Response.Replace("<" + ValueToSearch + ">", ValueToSearch);
Response = Response.Replace("</" + ValueToSearch + ">", ValueToSearch);
}
// Response = "actionPlayaction<Speed>1</Speed><blah>lol</blah>"
int indexOfWord = Response.IndexOf(ValueToSearch); // indexOfWord = 0
int start = indexOfWord + ValueToSearch.Length; // start = 6
int end = Response.Length - indexOfWord - 1; // 47
int totalLength = Response.Length; // 48
string newPositionInfo = "";
if (indexOfWord == -1) {
return "";
} else {
newPositionInfo = Response.Substring(start, totalLength - start); // newPositionInfo = "Playaction<Speed>1</Speed><blah>lol</blah>"
indexOfWord = newPositionInfo.IndexOf(ValueToSearch); // indexOfWord = 4
returnValue = newPositionInfo.Substring(0, indexOfWord); // returnValue = "Play"
if (RemoveHtmlCharacters) {
returnValue = returnValue.Replace("<", "");
returnValue = returnValue.Replace(">", "");
returnValue = returnValue.Replace("&", "");
}
return returnValue; // "Play"
}
}
这段代码的用法如下: - 我想要获取“行动”一词之间的一切。 - 我有一个包含单词“action”的文本。
string largeText = "<action>Play</action><speed>1</speed><blah>blah</blah>"
string wordToSearch = "action"
string value1 = GetInformationFromResponse(largeText, "action");
string value2 = GetInformationFromResponse(largeText, "speed");
string value3 = GetInformationFromResponse(largeText, "blah");
// Value 1 = "Play"
// Value 2 = "1"
// Value 3 = "blah"
但是我如何在Lua(在我的NodeMCU上)完成同样的事情?
注:Lua和NodeMCU
谢谢!奇迹般有效! – Johnnybossboy