Elegant Way Of Shorten A Text String
Join the DZone community and get the full member experience.Join For Free
this method shortens a plain text string down to complete words contained in given scope (count)
def shorten (string, count = 30) if string.length >= count shortened = string[0, count] splitted = shortened.split(/\s/) words = splitted.length splitted[0, words-1].join(" ") + ' ...' else string end end
Strings Data Types
Opinions expressed by DZone contributors are their own.