Last active
August 29, 2015 14:07
-
-
Save edvardm/4689ab0a0e881c2ae38d to your computer and use it in GitHub Desktop.
break string in the middle or after of the sentence with <br>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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