Ta©ti Tac0Z 2 Posted December 29, 2016 Share 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 Link to post Share on other sites
0 Solution Fingercomp 37 Posted January 6, 2017 Solution Share 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 Link to post Share on other sites
0 ATastyPeanut 0 Posted January 5, 2017 Share 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 Link to post Share on other sites
0 Ta©ti Tac0Z 2 Posted January 5, 2017 Author Share 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 Link to post Share on other sites
0 Ta©ti Tac0Z 2 Posted January 8, 2017 Author Share Posted January 8, 2017 Thank. Let me try it Quote Link to post Share on other sites
0 Ta©ti Tac0Z 2 Posted January 8, 2017 Author Share Posted January 8, 2017 I Will try it now i mean Quote Link to post Share on other sites
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
Link to post
Share on other sites