Given two parameters… STRING, INTEGER
return an Array with the text of the passed STRING broken into segments no more than INTEGER characters long, honoring any CRLF that may exist, and lines ending on word barriers.
This is to be able to output the text as word-wrapped into a textfile, where the column width is a specific value
NOW IS THE TIME FOR ALL GOOD MEN TO COME TO THE AID OF THEIR COUNTRY
would return (if given a width of 20)
NOW IS THE TIME FOR
ALL GOOD MEN TO
COME TO THE AID OF
THEIR COUNTRY
need to know what you define “word barriers” as ?
spaces ?
what punctuation ?
are the words or phrases that should not be split (ie/ “de facto” probably should be split)
I think I got it :). Not going to worry about “messy” situations… just break on word boundary … here is the Swift code (and darn if it didn’t work the 1st time!)
func getNoteArray(_ key : String,maxChar:Int) -> [String] {
let inText = Split(getNote(key),"\n") // return Text from database based on "key"
var outText = [String]()
for i in(0...inText.count-1) {
var s = Trim(inText[i])
if s.count<=maxChar {
outText.append(s)
} else {
while(s.count)>maxChar {
for x in(1...maxChar).reversed() {
let c=Mid(s,x,1)
if InStr(" !)]};:,.=/<>?",c)>0 {
outText.append(Left(s,x))
s=Trim(Mid(s,x+1))
break
}
}
}
if s.count>0 { outText.append(s) }
}
}
// dump result for debug purposes
for i in(0...outText.count-1) {
print("\(i)=\(outText[i])")
}
return outText
}
If anyone comes up with a better method, I’m open
Note : even though this is SWIFT code… the functions Left, Mid, Instr etc are from a String library I wrote to make things more “comfortable”