Skip to content

Instantly share code, notes, and snippets.

@edvardm
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edvardm/4689ab0a0e881c2ae38d to your computer and use it in GitHub Desktop.
Save edvardm/4689ab0a0e881c2ae38d to your computer and use it in GitHub Desktop.
break string in the middle or after of the sentence with <br>
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..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment