Ta©ti Tac0Z Posted December 29, 2016 Posted December 29, 2016 hallo this is more then a lua question but i need to spit a string in a "specific amount of characters" i will use the: string.gmatch(string, "but what do I put in here???") I've looked in the lua wiki but I could not find it there Quote
0 Fingercomp Posted January 6, 2017 Posted January 6, 2017 local function splitEvery(str, n) local result = {} for i = 1, #str, n do local substr = str:sub(i, i + n - 1) table.insert(result, substr) end return result end print(table.unpack(splitEvery("abcdefghi", 3))) --> abc def ghi print(table.unpack(splitEvery("hello, world!", 4))) --> hell o, w orld ! local function splitHalf(str) local leftLen = math.ceil(#str / 2) return str:sub(1, leftLen), str:sub(leftLen + 1, -1) end print(splitHalf("abcdef")) --> abc def print(splitHalf("helloworld")) --> hello world print(splitHalf("programming")) --> progra mming Quote
0 ATastyPeanut Posted January 5, 2017 Posted January 5, 2017 I'm going to assume you meant to say that you're trying to split a string, not that you're trying to spit a string. Then my next question is how do you want to split it? Will the split be in the same location all the time? Will there only be one split with two output strings or are you trying to split the string into multiple parts? Like do you want to split the string into a bunch of individual words? Then what do you want to do with the strings? Maybe load each word into a table? Please be clearer with what you are trying to do/accomplish. Quote
0 Ta©ti Tac0Z Posted January 5, 2017 Author Posted January 5, 2017 Hallo thanks You to help me and yes not spit but split and i will do it like this Input_string = "abcareyougod" Data = string.gmatch(input_string, "'and now i Will write a code in here that Will split the string in every 3 characters or what amunt i need but in this case 3'") while true do text = Data() if text==nil then break end print(text) end ------------------------ output: abc are you god ------------------------------- And if You Can Will You then also tell me how to split it in to 2 strings like i mean in half Quote
hallo this is more then a lua question but i need to spit a string in a "specific amount of characters" i will use the:
I've looked in the lua wiki but I could not find it there