简体   繁体   English

Lua 中的字符串操作

[英]string operations in Lua

I have a string in which I have to replace some characters (or to delete, which is the same here).我有一个字符串,我必须在其中替换一些字符(或删除,这里也是一样)。 The important part is marked by the character @ .重要的部分由字符@标记。 the charcters before it should be replaced by the following ones, independet on the number of characters before and behing the @ .前面的字符应替换为以下字符,独立于@之前和后面的字符数。 The end of the string on the right side of @ is given by line end or the characters ! @右侧字符串的结尾由行尾或字符! and || . . Some examples一些例子

str0 = "whatever"                 -- do nothing
str1 = "whatever!whenever!time"   -- do nothing
--
-- the following should replace only "little" by "big"
-- means "little@big"  -> "big"
--
str2 = "little@big"  
str3 = "Size!little@big"
str4 = "Whatever!Size!little@big!whatever"
str5 = "Whatever!little@big!whatever!little@big"
str5 = "Whatever!little@big!whatever!little@big!little@big"
str6 = "Whatever!little@big!whatever!little@big!little@big|foo"

the numbers of @ is not known. @的数量未知。 Is there an "easy" code to modify the string?是否有“简单”的代码来修改字符串?

str = str:gsub('[^!|@%s]+@', '')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM