I have a string for which the last (dot) need to replaced with "_"
x = "W1-9007-abc_1.0.ns" -- Other example: W1-9007-abc_1.0.0.ns -> W1-9007-abc_1.0.0_ns
Expected Output:
x = "W1-9007-abc_1.0_ns"
Reference:
I have tried this reference without much luck.
Reference: stackoverflow.com/a/45263473/2986344
Solution that worked:
x = "W-9007-mnist_1.0.0.ms"
x = x:gsub("(.*)[%p]", "%1_")
print(x)
Alternative solution:
local before_dot, after_dot = str:match"(.*)%.(.-)$"
str = before_dot .. "_" .. after_dot
Related
So what I want to do is pass a variable into an eval_ by address and then set the variable in there like so:
string str = "a"
Addr64_ addr = addr_ str
eval_("string ref = addr_ " addr "; ref = \"b\"")
print str // I want it to print b here
I realize I could evalTop_ here to make the top variable available to the eval so it can set it, but this is not the behavior that I am looking for.
So after a lot of debugging and combining different answers from the following sources:
Memory hacks 1
Memory hacks 2
I found a solution! the following code should work for basically any variable:
bool a = false
eval_("bool &a = addr_ " (addr_ (&a)) "; a = true")
print a // should print true
I have something like
str = "What a wonderful string //011// this is"
I have to replace the //011// with something like convertToRoman(011) and then get
str = "What a wonderful string XI this is"
However, the conversion into roman numbers is no problem here.
It is also possible that the string str didn't has a //...//. In this case it should simply return the same string.
function convertSTR(str)
if not string.find(str,"//") then
return str
else
replace //...// with convertToRoman(...)
end
return str
end
I know that I can use string.find to get the complete \\...\\ sequence. Is there an easier solution with pattern matching or something similiar?
string.gsub accepts a function as a replacement. So, this should work
new = str:gsub("//(.-)//", convertToRoman)
I like LPEG, therefore here is a solution with LPEG:
local lpeg = require"lpeg"
local C, Ct, P, R = lpeg.C, lpeg.Ct, lpeg.P, lpeg.R
local convert = function(x)
return "ROMAN"
end
local slashed = P"//" * (R("09")^1 / convert) * P"//"
local other = C((1 - slashed)^0)
local grammar = Ct(other * (slashed * other)^0)
print(table.concat(grammar:match("What a wonderful string //011// this is"),""))
I have a lua string in Chinese, such as
str = '这是一个中文字符串' -- in English: 'this is a Chinese string'
Now I would like to iterate the string above, to get the following result:
str[1] = '这'
str[2] = '是'
str[3] = '一'
str[4] = '个'
str[5] = '中'
str[6] = '文'
str[7] = '字'
str[8] = '符'
str[9] = '串'
and also output 9 for the length of the string.
Any ideas?
Something like this should work if you are using utf8 module from Lua 5.3 or luautf8, which works with LuaJIT:
local str = '这是一个中文字符串'
local tbl = {}
for p, c in utf8.codes(str) do
table.insert(tbl, utf8.char(c))
end
print(#tbl) -- prints 9
I haven't used non-english characters in lua before and my emulator just puts them in as '?' but something along the lines of this might work:
convert = function ( str )
local temp = {}
for c in str:gmatch('.') do
table.insert(temp, c)
end
return temp
end
This is a simple function that utilizes string.gmatch() to separate the string into individual characters and save them into a table. It would be used like this:
t = convert('abcd')
Which would make 't' a table containing a, b, c and d.
t[1] = a
t[2] = b
...
I am not sure if this will work for the Chinese characters but it is worth a shot.
Is there a way to convert or create a new [[bracket style string]] based on an existing 'quote style string'?
s = "one\ntwo" -- how the string was created
s2 = [[one\ntwo]] -- what i want the new string to be
Escaping the escape sequence seems to achieve the desired effect, at least in this case.
s2 = string.gsub(s, "\n", "\\n")
> print(s2)
one\ntwo
One way is to make a table that has all the possible escape sequences:
local t = {["\a"] = [[\a]],
["\b"] = [[\b]],
["\f"] = [[\f]],
["\n"] = [[\n]],
["\r"] = [[\r]],
["\t"] = [[\t]],
["\r"] = [[\r]],
["\\"] = [[\\]],
["\""] = [["]],
["\'"] = [[']],
}
local s2 = s:gsub(".", t)
How do you get the last index of a character in a string in Lua?
"/some/path/to/some/file.txt"
How do I get the index of the last / in the above string?
index = string.find(your_string, "/[^/]*$")
(Basically, find the position where the pattern "a forward slash, then zero or more things that aren't a forward slash, then the end of the string" occurs.)
This method is a bit more faster (it searches from the end of the string):
index = your_string:match'^.*()/'
Loops?!? Why would you need a loop for that? There's a 'reverse' native string function mind you, just apply it then get the first instance :) Follows a sample, to get the extension from a complete path:
function fileExtension(path)
local lastdotpos = (path:reverse()):find("%.")
return (path:sub(1 - lastdotpos))
end
You can do it in a single line of course, I split it in two for legibility's sake.
Here is a complete solution.
local function basename(path)
return path:sub(path:find("/[^/]*$") + 1)
end
local s = "/aa/bb/cc/dd/ee.txt"
local sep = "/"
local lastIndex = nil
local p = string.find(s, sep, 1)
lastIndex = p
while p do
p = string.find(s, sep, p + 1)
if p then
lastIndex = p
end
end
print(lastIndex)
You could continue find next value, until find nil,record last position