This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
FuuuAInfiniteLoop(F.A.I.L)'s profile picture

split "%" from a string

Started by FuuuAInfiniteLoop(F.A.I.L), 28 February 2013 - 09:03 AM
FuuuAInfiniteLoop(F.A.I.L) #1
Posted 28 February 2013 - 10:03 AM
anyonw knows how to split the % character from a string?
FuuuAInfiniteLoop(F.A.I.L) #2
Posted 28 February 2013 - 10:17 AM
I believe the code is like

tbl = {}
for word in string.gmatch(str, "%%a%") do print(word) end


Edit dont works :(/>
oeed #3
Posted 28 February 2013 - 10:26 AM
Do you mean if you had string, say "Test 123%456" you could remove it, therefore making it "Test 123456"?

If so, do this:


aString = "Test 123%456"
aString = string.gsub("%%", "")
-- aString is now Test123456
Watcher7 #4
Posted 28 February 2013 - 10:35 AM
Do you mean split a string apart based on the placement of "%"s?
http://codepad.org/wlMBsKIy

str = "Hello%world%foo%bar"
tbl = {}
str:gsub("[^%%]+", (function(word) table.insert(tbl, word) end))
or (in the form of a function)
http://codepad.org/Q4QnRUyG

split_function = function(str)
    local tbl = {}
    str:gsub("[^%%]+", (function(word) table.insert(tbl, word) end))
    return tbl
end
FuuuAInfiniteLoop(F.A.I.L) #5
Posted 28 February 2013 - 10:51 AM
I mean this
str = lol%lol%rsd%asd%fds

i want to get the parts in bold, the quantity and the order can change as well as the contents

lol, rsd and asd are keys in a table that will be replaced

i want to use that string or %f%fs%sjdfjs%%ff%f%s% always getting the text that are in %%
Lyqyd #6
Posted 28 February 2013 - 11:21 AM
string.match(str, "%%([^%%]+)%%") may do what you want. You'd probably want to use a string.gmatch loop, though.
oeed #7
Posted 28 February 2013 - 01:16 PM
If none of the above work, take a look on Google or StackOverflow for 'split string lua'.
SuicidalSTDz #8
Posted 28 February 2013 - 02:25 PM
Does no one read the Lua Manual anymore? All these answers are in there under String Munipulation (Pretty self-explanatory)