第一个问题.在Lua中确定字符串中的最后一个字符是否不是多字节的最简单方法是什么.或者从字符串中删除最后一个字符的最简单方法是什么.
以下是有效字符串的示例,以及我想要的函数输出
hello there --- result should be: hello ther
anñ --- result should be: an
???? --- result should be: ???
????? --- result should be: ????
Run Code Online (Sandbox Code Playgroud)
我需要类似的东西
function lastCharacter(string)
--- some code which will extract the last character only ---
return lastChar
end
Run Code Online (Sandbox Code Playgroud)
或者如果它更容易
function deleteLastCharacter(string)
--- some code which will output the string minus the last character ---
return newString
end
Run Code Online (Sandbox Code Playgroud)
这是我正在进行的道路
local function lastChar(string)
local stringLength = string.len(string)
local lastc = string.sub(string,stringLength,stringLength)
if lastc is a …Run Code Online (Sandbox Code Playgroud) 假设我正在创建一个注册表单.我有类似下面的代码,因为所有嵌套的if语句都很难管理.
我想知道最干净,最简单的方法来编写与我下面的代码类似的代码.
编辑:人们告诉我,我可以将空($ _ POST ['email'])移动到验证功能.我不能这样做,因为我需要知道1)用户是否发布了数据,以及2)用户发布的数据是否有效.
例如,当用户首次进入注册页面时,他们没有发布任何数据,因此$ _POST ['email']将生成PHP警告,因为它们不存在.这就是我在验证之前检查数据是否已发布的原因.
这有意义吗?
function validate_email($str) {
$str = trim(strtolower($str));
if(!filter_var($str, FILTER_VALIDATE_EMAIL)) {
return false;
} else {
return $str;
}
}
function validate_password($str) {
$str = trim($str);
if(strlen($str) < 5 || strlen($str) > 70) {
return false;
} else {
return $str;
}
}
$email = false;
$password = false;
$errorMessage = false;
if(!empty($_POST['email'])) {
$email = validate_email($_POST['email']);
if($email) {
if(!empty($_POST['password'])) {
$password = validate_password($_POST['password']);
if($password) {
createNewUser($email,$password);
} else {
$errorMessage …Run Code Online (Sandbox Code Playgroud) 我正试图找出在我的应用程序中修复内存泄漏并使用外部类的最佳方法.我正在使用Corona SDK进行编码并使用Storyboard.当我通过类创建它时,我不认为我正在正确地删除对象.你能看看并帮助以下
1)在代码的底部,我展示了如何移除键盘.这是否足够,或者我需要做更多,因为键盘是通过另一个文件创建的?
2)在keyboard.lua中,我需要以keyboard.lua文件稍后可以使用函数操作它们的方式创建对象.我通过声明Keyboard,theCursor,thebackground来做到这一点.
是否有任何理由这样做,称他们为M.theKeyboard,M.theCursor,M.theBackground而不提前宣布他们,因为M是本地的?
3)你会以不同的方式实现这个键盘类吗?如果是的话,你能指点一下吗?
这是示例代码.我想在我的应用程序中重用此键盘代码.任何时候都应该只有一个键盘.我想在用户退出场景时完全移除键盘,因为许多屏幕不需要键盘.
-- keyboard.lua
local M = {}
local theKeyboard, theCursor, theBackground
function M.newBackground()
if theBackground then
theBackground = nil
end
local newBackground = display.newRect(0,0,0,0)
-- set position, size, color, etc
theBackground = newBackground
return newBackground
end
... many other functions to create cursor, textlabels, etc
function M.newKeyboard()
if theKeyboard then
theKeyboard = nil
end
theKeyboard = display.newGroup()
theCursor = M.newCursor()
theBackground = M.newBackground()
-- lots more stuff... like I create buttons for each key …Run Code Online (Sandbox Code Playgroud) 我试图将一个函数作为参数传递给另一个函数.
高级别我有代码创建一个弹出窗口.当我用新文本更新弹出窗口时,我还想更新用户单击弹出窗口时发生的操作.例如,我第一次更新弹出窗口时,我可能会将Action更改为再次使用新文本显示弹出窗口.当用户点击第二个时
下面是一些示例代码来说明这个概念
function doSomething()
print("this is a sample function")
end
function createPopup()
local popup = display.newRect ... create some display object
function popup:close()
popup.isVisible = false
end
function popup:update(options)
if options.action then
function dg:touch(e)
-- do the action which is passed as options.action
end
end
end
popup:addEventListener("touch",popup)
return popup
end
local mypopup = createPopup()
mypopup:update({action = doSomething()})
Run Code Online (Sandbox Code Playgroud) coronasdk ×3
lua ×3
function ×1
if-statement ×1
memory-leaks ×1
multibyte ×1
nested ×1
oop ×1
php ×1
regex ×1