[英]Reverse String in Lua?
I am trying to reverse a string in Lua without using the string.reverse() function. 我试图不使用string.reverse()函数来反转Lua中的字符串。 Here is my code -
这是我的代码-
function reverseStr(s)
return string.gsub(s, "(.)(.)", "%2%1")
end
The code is currently only reversing the first two characters in the string, and I am wondering what I can do to make the function reverse every character in the string. 该代码当前仅反转字符串中的前两个字符,我想知道如何使函数反转字符串中的每个字符。
abc -- cba bbc -- cbb dka -- akd abc-cba英国广播公司-cbb dka-akd
Thank you! 谢谢!
You can't make Lua's pattern matching system reverse a string. 您不能使Lua的模式匹配系统反转字符串。 You have to either write the obvious reversing code (iterating through the string backwards, building a new table from strings in reverse order, and using
table.concat
) or use string.reverse
. 您必须编写明显的反向代码(向后迭代字符串,以相反的顺序从字符串构建新表,并使用
table.concat
)或使用string.reverse
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.