‘.’ represents a single character. ‘string.rep’ repeats it n times, so that the outcome pattern will match n chars.
There’s a little problem with Kogitsune’s code, though. It will only match n characters and not less. This one will split the string properly…
[lua]
function string.chop( str, size )
local output = { };
local pattern = string.rep( ".", size );
str = str:gsub( pattern, function( part ) table.insert( output, part ); return ""; end );
if( #str > 0 ) then table.insert( output, str ); end
return output;
end
[/lua]
Here’s a library with some useful string functions if you need anything else.
Edit:
Ha. Actually, you can use Kogitsune’s code, but with a different pattern. You can use the ‘?’ repetition which will match zero or one rep…
[lua]
function string.split( str, size )
local output = { };
for match in str:gmatch( "."..string.rep( ".?", size - 1 ) ) do
if( #match > 0 ) then table.insert( output, match ); end
end
return output;
end
[/lua]