EdvardM
9/30/2014 - 12:26 PM

break string in the middle or after of the sentence with <br>

break string in the middle or after of the sentence with

def break_after_middle(s)
  midpoint = s.length/2-1
  idx_after_middle = s[midpoint..-1].index(' ')

  if idx_after_middle
    idx =  idx_after_middle + midpoint
    s.dup.tap { |p| p[idx..idx] = '<br/>' }
  else
    s
  end
end

break_after_middle 'Breaks in the midst' # => "Breaks in<br/>the midst        That's not proper English, sure.
break_after_middle 'In a galaxy long, long time ago, far, far away...' # => "In a galaxy long, long time<br/>ago, far, far away..."