我有以下Lua代码我希望转换为C#。我唯一的问题是字符串操作。我不知道如何将它们翻译成C#:
if (classaddress~=nil) and (classname~=nil) then
classname=classname:match "^%s*(.-)%s*$"
if (classname~='') then
local r=string.find(classname, "[^%a%d_.]", 1)
if (r==nil) or (r>=5) then
return currentaddress, classaddress, classname
end
end
end
答案 0 :(得分:3)
代码中的字符串为patterns。它们就像正则表达式,但有点不那么强大。第一个匹配匹配整个字符串,不包括字符串开头和结尾的空格,因此它的行为类似于Trim
。第二个模式用于查找不是字母,数字,下划线(_
)或点(.
)的第一个字符。转换为C#,它可能看起来像这样:
public class MyReturnObject {
public MyReturnObject(int currentAddress, int classAddress, string className) {
// ...
}
}
public MyReturnObject ParseStuff(int? classAddress, string className, int currentAddress) {
if(classAddress.HasValue && className != null) {
className = className.Trim();
if(!String.IsNullOrEmpty(className)) {
var r = System.Text.RegularExpressions.Regex.Match(className, @"[^\a\d_.]")?.Index;
if(!r.HasValue || r.Value >= 5) {
return new MyReturnObject(currentAddress, classAddress.Value, className);
}
}
}
return null;
}