Let string_a = "I'm a string but this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa is long."
How is it possible to detect the aaaa...aaa
(the part without spaces that is very long) part and truncate it so that the result looks like (the rest of the string should look the same):
puts string_a # => "I'm a string but this aaa....aaa is long."
I use this method to truncate:
def truncate_string(string_x)
splits = string_x.scan(/(.{0,9})(.*?)(.{0,9}$)/)[0]
splits[1] = "..." if splits[1].length > 3
splits.join
end
So running:
puts truncate_string("I'm a very long long long string") # => "I'm a ver...ng string"
The problem is detecting the 'aaaa...aaaa'
and apply truncate_string
to it.
First part of the solution? Detecting strings that are longer than N using regex?