Created
July 7, 2016 19:57
-
-
Save jbratu/67ec2295be958f960a350760123ef7aa to your computer and use it in GitHub Desktop.
Given a string of words the function breaks the string into chunks no longer than the specified length. Breaks on word boundaries and avoids breaking words into fragments.
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
Function CS_BREAKON_BOUNDARY(Sentence, Boundary, BreakLength) | |
/* | |
Example: | |
Sentence = 'The Quick Brown Fox................ Jumped Over the Lazy Dog ....' | |
Lines = CS_BREAKON_BOUNDARY(Sentence, ' ', 10) | |
Lines contains an array of sentences And words that do Not exceed 10 characters In length. | |
*/ | |
Lines = '' ;* Store our broken sentence lines for return | |
Line = '' ;* Current compiled line | |
Word = '' ;* Current word | |
*Loop through all the chars in sentence | |
SentenceLen = Len(Sentence) | |
For i = 1 To SentenceLen | |
*Get one char | |
C = Sentence[i,1] | |
If C EQ Boundary Then | |
*This is a word boundary | |
If (Len(Word) + Len(Line)) < BreakLength Then | |
*The word and current line is less than our break length so we | |
*can add the current word onto the current line | |
If Line EQ '' Then | |
Line = Line : Word | |
End Else | |
Line = Line : Boundary : Word | |
End | |
Word = '' | |
End Else | |
*The current line and word exceed the break length so we | |
*add the current line to lines and the word becomes a new line | |
Lines<-1> = Line | |
Line = Word | |
Word = '' | |
End | |
End Else | |
*No special character, keep building a word | |
Word := C | |
End | |
If Len(Word) = BreakLength Then | |
*Hard break, the word is longer than the break length | |
Lines<-1> = Line | |
Line = '' | |
Lines<-1> = Word | |
Word = '' | |
End | |
Next | |
*End loop | |
*Clean up remainder Word and Line | |
If (Len(Word) + Len(Line)) < BreakLength Then | |
*Add to final line, length allows | |
Lines<-1> = Line : Boundary : Word | |
End Else | |
*Word and line exceed max length so add each to a separate line | |
Lines<-1> = Line | |
Lines<-1> = Word | |
End | |
Return Lines |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment