76451

Split string into table in groups of 26 characters or less, rounded to the nearest word

I'm trying to take a string and split it into a table, with each index containing a maximum of 26 characters, but words are not split between indices. If it stretches over 26 characters mid-word, that word is in the next index.

Answer1:

I've answered a similar question some time ago; I'm sure you can get the suggested implementation and use it for your case. You just need to return the table instead of doing the concatenation:

local function formatUpToX(s) local x = 26 local splitstr = "([ \t]*)(%S*)([ \t]*)(\n?)" local t = {""} for prefix, word, suffix, newline in s:gmatch(splitstr) do if #(t[#t]) + #prefix + #word > x and #t > 0 then table.insert(t, word..suffix) else t[#t] = t[#t]..prefix..word..suffix end if #newline > 0 then table.insert(t, "") end end return t end

Answer2:

Words longer than wrap_length are the only words that will overflow, but that behavior can be customized by replacing or str:find'%s'.

local function wrap_string(str, wrap_length) local lines = {} while #str > 0 do local last_space_index -- Only check substrings longer than the wrap length if #str > wrap_length then -- Check for first space so words longer than `wrap_length` don't break. last_space_index = str:sub(1, wrap_length):find'%s%S*$' or str:find'%s' end table.insert(lines, str:sub(1, last_space_index)) if not last_space_index then break end str = str:sub(last_space_index + 1) end return lines end

Recommend

  • JS Cannot read property “length” of undefined [closed]
  • Compile ffmpeg as stand alone binary
  • Wiring top-level DAGs together
  • Applescript - creating a folder if it doesn't exist
  • Android ContentProvider applyBatch not saving all contact data
  • Calculate the depth of subclass in the OWL ontology
  • How to print a java String in a jsp file [duplicate]
  • Best modern way of creating indentation in a ?
  • Swift 3 - Pass struct by reference via UnsafeMutableRawPointer?
  • Retrieve the name of the value a Scala macro invocation will be assigned to
  • jaxb xsd prefix package names
  • How does extglob work with shell parameter expansion?
  • Search files(key) in s3 bucket takes longer time
  • What does Main.1 output file do in XCode?
  • Criterion causing memory consumption to explode, no CAFs in sight
  • abstracting over a collection
  • Can't remove headers after they are sent
  • Validate child input components on submit with Vee-Validate and vue js 2
  • Intel-64 and ia32 atomic operations acquire-release semantics and GCC 5+
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • How to define and use opencv mat of user type
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • How to make Safari send if-modified-since header?
  • How to pass list parameters for each object using Spring MVC?
  • Arrays break string types in Julia
  • log4net write single file for each call to log.info
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • JaxB to read class hierarchy
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?