Regards
Augustas
function splitToLines(string, area)
local str = {} local line = 1
local area = area or 51
for i = 1, #string, area do
str[line] = string:sub(i, i + (area - 1))
end
return str
end
function splitToLines(string, area)
local str = {}
for i = 1, #string, area or 51 do
str[#str+1] = string:sub(i, i + (area - 1))
end
return str
end
local function parseLines(text,constraint)
local text = text:gsub("\n"," ")
local words = {}
local word = ""
for i = 1, #text do
if text:sub(i,i) == " " then
table.insert(words, word)
word = ""
else
word = word .. text:sub(i,i)
end
end
table.insert(words, word)
local lines = {}
local line = ""
for i,v in ipairs(words) do
if #line + #v + 1 > constraint then
table.insert(lines, line)
line = v
else
if line ~= "" then
line = line .. " " .. v
else
line = v
end
end
end
table.insert(lines,line)
for k,v in pairs(lines) do
if v == "" then lines[k] = nil end
end
return lines
end
Though to be honest, if you're only interested in specific lengths, you should just stick to what you currently have.For the record, here's the function i usestring.lineFormat = function(text,lineLength,center) local tLines = {} while #text > 0 do --splits text into a table containing each line local line = text:sub(1,lineLength) local newLine = string.find(line.."","\n") --check for new line character if newLine then line = line:sub(1,newLine-1) text = text:sub(#line+2,#text) elseif #line == lineLength then local endSpace = line:find"%s$" or line:find"%s%S-$" or lineLength line = line:sub(1,endSpace) text = text:sub(#line+1) else text = "" end if center then line = string.rep(" ",math.max(math.floor((lineLength-#line)/2),0))..line line = line..string.rep(" ",math.max(lineLength-#line,0)) end tLines[#tLines+1] = line end return tLines end
Not faster, I'm saying perhaps there are certain situations where table.insert should be used to get a table that may is better arranged, perhaps there are some types of tables or something that need table.insert, I'm not sure just guessing, because table.insert is designed for insertion perhaps there inserting without giving the place has more than just doing table[#table + 1] to it. Speed is my concern only if I am 100% sure that the way I'm doing can be faster but with the same outcomes in all situations.
Maybe there's not, you see I didn't know that, and that explains why I said, I'm not sure, speed is only my concern if I'm 100% sure, I don't know the code for table.insert. Therefore I took a guess, and JUST incase I used it, just INCASE IF there actually IS anything special about it.
I appreciate you telling me about table.insert, and I will change it. But, I'm just trying to explain myself, and I capitalise words to indicate the exact thing I'm trying to explain, certain grammar and word layout can mean different things even if it looks identical. But, again, thanks :)/>/>
Should I make a new topic asking about splitting lines by the given area based on words? Because it uses a different method, and I'm eager to find out what could be the most efficient way.
Regards
Augustas
Both my function and CometWolf's do this. Go back a page.New Question
How could I create a function that splits the given string into lines by the given width based on words, a good example would be a word document, if you write a series of words, the last word on the line if too long will not be split in-half and the other half being on a new line, the word goes to the new line, but if you have a word that is longer than the entire width then it will actually split, how could I create this?
Example
Width is 10 in these examples
————–
This is the way of tommorow
=
This is
the way
of
tommorow
—————
Lua programming is okay
=
Lua
programmin
g is okay
—————
How could I do this?
Regards
Augustas
Both my function and CometWolf's do this. Go back a page.New Question
How could I create a function that splits the given string into lines by the given width based on words, a good example would be a word document, if you write a series of words, the last word on the line if too long will not be split in-half and the other half being on a new line, the word goes to the new line, but if you have a word that is longer than the entire width then it will actually split, how could I create this?
Example
Width is 10 in these examples
————–
This is the way of tommorow
=
This is
the way
of
tommorow
—————
Lua programming is okay
=
Lua
programmin
g is okay
—————
How could I do this?
Regards
Augustas
function k.splitWordLines(text, area, newline)
k.validType("string", text)
local area = area or k.findW()
local words = k.splitWords(text)
local split = {}
for i, word in ipairs(words) do
if #word > area then
table.insert(words, i + 1, word:sub(area + 1))
words[i] = word:sub(1, area)
end
end
for i, word in ipairs(words) do
if split[1] == nil then split[1] = word
elseif #(split[#split]..word) > area then
split[#split + 1] = word
else
split[#split] = split[#split]..word
end
if #split[#split] < area then
split[#split] = split[#split].." "
end
end
if newline == true then
for i, line in pairs(split) do
if line:find("\n") then
local new = k.splitNewline(line)
for ins = 1, #new do
table.insert(split, ins - 1, new[i])
end
end
end
end
end