View Single Post
02-01-10, 05:15 PM   #2
ArrchDK
A Fallenroot Satyr
 
ArrchDK's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 25
Code:
MAX_CHAR = 230
local myMsg = "This is my message..." -- Message you want to break up.
local myStringTbl = {} -- Table to store each line.

local function breakUpLine(msg)

	if string.len(msg) < MAX_CHAR then -- If our string is less than our per line char limit, then insert the whole line into table.
		tinsert(myStringTbl, "..."..msg)
	else
		index = MAX_CHAR -- Start at the MAX_CHAR character
		while (string.sub(msg, index, index) ~= " ") -- Go back character by character until we find a space.
			index = index - 1
		end

		tinsert( myStringTbl, "..."..string.sub(msg, 1, index - 1).."..." ) -- Insert a line into our table from the beginning of the string to character before the space.
		breakUpLine(string.sub(msg, index+1)) -- Run recursively, passing the string that starts from after our space character until the end.
	end
end

breakUpLine(myMsg) -- Run our recursive function.
myStringTbl[1] = string.sub(myStringTbl[1], 4) -- Removes the preceding ellipsis from the first line.
Untested, but it should do the trick.

When finished, you'll have a table with indeces of 1, 2, ... n where n is the number of lines:
myStringTbl[1] = "This is a string that..."
myStringTbl[2] = "...has been split up..."
myStringTbl[3] = "...based on a predetermined..."
myStringTbl[4] = "...number of max characters..."
myStringTbl[5] = "...per line stored in MAX_CHAR"

Last edited by ArrchDK : 02-01-10 at 05:28 PM. Reason: Add more info & clarification on the code.
  Reply With Quote