Read only the last characters of a LUA string

4

I have a string, I just want to read the last characters after the last ";".

Example: "12/5/2015; 00: 00: 05; 90" -> Read only the "90"

    
asked by anonymous 12.05.2015 / 16:41

2 answers

3

Read 2 last characters:

str = "12/5/2015;00:00:05;90"
 print(string.sub(s, -2))

Break into 3:

str = '12/5/2015;00:00:05;90'
for word in string.gmatch(str, '([^;]+)') do
    print(word)
end

Placing each part in an Array:

 a = {} -- array 1
 b = {} -- array 2
 c = {} -- array 3

str = '12/5/2015;00:00:05;90'
op = 0
for word in string.gmatch(str, '([^;]+)') do
   op = op+1
   if op == 1 then
      a[1] = word
   elseif op == 2 then
      b[1] = word
   elseif op == 3 then
      c[1] = word
   end
end
print(a[1])
print(b[1])
print(c[1])

You can run the code and test online here

    
12.05.2015 / 17:57
1

Try this:

s = "12/5/2015;00:00:05;90"
print(s:match(".*;(.+)$"))

.*; advances to the last ; .

(.+)$ captures and returns the remainder to the end of the string.

    
19.05.2015 / 20:58