Created
October 12, 2012 17:34
-
-
Save shamil614/3880427 to your computer and use it in GitHub Desktop.
Split up a string by words using a character limit.
This file contains 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
class String | |
def chunk_by_words(char_count) | |
words = self.split | |
s1_len = 0 | |
word_count = 0 | |
i = 0 | |
while (i <= words.count-1) | |
s1_len += words[i].length | |
word_count += 1 | |
i+=1 | |
break if s1_len >= char_count | |
end | |
[words[0..word_count-1].join(' '), words[word_count..words.count].join(' ')] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment