28618

Split string into chunks (of different size) without breaking words

I am trying to create a method that, given a string, returns three strings: title, description1, description2

This is a related question I found: Split a string into chunks of specified size without breaking words - But my chunks are of different size.

Title needs to be max 25 characters.

Description1 needs to be max 35 characters.

Description2 needs to be max 35 characters.

The question would be:

How can I split a string so that it creates a maximum of three entities (<strong>NOTE</strong>: If the string can fit in just the first entity that is OK, I don't need to return the three entities), where first entity has a maximum of 25 characters and the other two have a max of 35 characters each. Making the method clever enough to take into account words (and maybe punctuation), so that it doesn't return cut results.

I have done the following:

def split_text_to_entities(big_string) title = big_string(0..24) description1 = big_string(25..59) description2 = big_string(60..94) end

But the problem with this approach is that if that if the input is "Buy our new brand shoes from our store. Best discounts in town and 40% off for first purchase.", the results would be:

title = "Buy our new brand shoes f" description1 = "rom our store. Best discounts in to" description2 = "wn and 40% off for first purchase."

And ideally they would be:

title = "Buy our new brand shoes" description1 = "from our store. Best discounts in" description2 = "town and 40% off for first"

So, try to split by character size, taking into account the words.

Answer1:

This doesn't do the trick?:

def get_chunks(str, n = 3) str.scan(/^.{1,25}\b|.{1,35}\b/).first(n).map(&:strip) end

Answer2:

To cover all the bases, I would do the following.

<strong>Code</strong>

def divide_text(str, max_chars) max_chars.map do |n| str.lstrip! s = str[/^.{,#{n}}(?=\b)/] || '' str = str[s.size..-1] s end end

(?=\b) is a (zero-width) positive lookahead that matches a word break, namely, a whitespace character or the end of the string.

<strong>Examples</strong>

max_nbr_chars = [25,35,35] str = "Buy our new brand shoes from our store. Best discounts in " + "town and 40% off for first purchase." divide_text(str, max_nbr_chars) #=> ["Buy our new brand shoes", # "from our store. Best discounts in", # "town and 40% off for first"] str = "Buy our new brand shoes from our store." divide_text(str, max_nbr_chars) #=> ["Buy our new brand shoes", "from our store.", ""] str = "Buy our new" divide_text(str, max_nbr_chars) #=> ["Buy our new", "", ""] str = "" divide_text(str, max_nbr_chars) #=> ["", "", ""] str = "Buyournewbrandshoesfromourstore." divide_text(str, max_nbr_chars) #=> ["", "Buyournewbrandshoesfromourstore.", ""] str = "Buyournewbrandshoesfromourstoreandshoesfromourstore." divide_text(str, max_nbr_chars) #=> ["", "", ""]

Note that if ^ were omitted from the regex:

str = "Buyournewbrandshoesfromourstore." divide_text(str, max_nbr_chars) #=> ["ewbrandshoesfromourstore.", "rstore.", ""]

Answer3:

s = "Buy our new brand shoes from our store. Best discounts in town and 40% off for first purchase." s =~ /\b(.{,25})\W+(.{,35})\W+(.{,35})\b/ [$1, $2, $3] # => # [ # "Buy our new brand shoes", # "from our store. Best discounts in", # "town and 40% off for first purchase" # ]

Recommend

  • C++: How do null characters work in std::string?
  • Remove standard english language stop words in Stanford Topic Modeling Toolbox
  • PHP: regular expression to remove `â` or `â€`?
  • How does the heap manager in java or C++ keep track of all the memory locations used by the threads
  • Refreshing i18n translated string interpolated values in Aurelia
  • how to UNSELECT row in a ttk.Treeview in tkinter
  • Getting proper map boundaries
  • How to resize image if the image is bigger than Textbox.But it not increase scale if smaller
  • True privateness in Python
  • Jhipster: How to create relationships with User entity using supplied tools?
  • JPA/EclipseLink Returning No Results
  • why 0.1+0.2-0.3= 5.5511151231258E-17 in php [duplicate]
  • Show records ordered with maximum price first in PHP & MySQL
  • Generating anchors with PyYAML.dump()?
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • How to merge keras sequential models with same input?
  • Symfony2 - Doctrine - no changeset in post update
  • Prevent page break in text block with iText, XMLWorker
  • Configure Spring's MappingJacksonHttpMessageConverter
  • Doctrine/Symfony entity generator and generating entity from one table
  • uml Composition relationships to RDF and OWL
  • How to autopopulate a field in SugarCRM form
  • Azure table store snapshot/backup capability
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • Why cepheus don't send int without quotes to orion?
  • GAE: Way to get reference to an HttpSession from its ID?
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • Force show.bind execution
  • xtable package: Skipping some rows in the output
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Change JButton Shape while respecting Look And Feel
  • Window Size for Mac application
  • Validaiting emails with Net.Mail MailAddress
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • 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?