Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Ta©ti Tac0Z

lua spit function use?

Question

5 answers to this question

Recommended Posts

  • 0
  • Solution
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

 

Link to post
Share on other sites
  • 0

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.

Link to post
Share on other sites
  • 0

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

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.