Split a string by multiple delimters and KEEP the delmiters

I need to tokenize a simple text string, the consists of “words” and “punctuation”
I need to split by the punctioan (including spaces) and return an array of all bits. In such a way that JOIN would return the original string

This.Is/a=Test

would return

["This", "." , "Is", "/", "a" , "=" , "Test"]

most Split functions toss out the delimiters, or only allow one

 let s: String = "Lorem Ipsum is simply dummy text "
        print(stringWidth(s))
        let v = s.split(separator: "")
        print(v)
["L", "o", "r", "e", "m", " ", "I", "p", "s", "u", "m", " ", "i", "s", " ", "s", "i", "m", "p", "l", "y", " ", "d", "u", "m", "m", "y", " ", "t", "e", "x", "t", " "]

which splits it into individual characters… which I can use, just slower
as I need to measure the width of each

here is what I came up with… does perfect wordWrapping (for the previous mentioned PDF class)

   private func wordWrap(_ text:String, columns: CGFloat = 80) -> String {
        var result    : String = ""
        var word      : String = ""
        let v               = text.split(separator: "")
        var buffer    : String = ""
        var bufferLen : CGFloat = 0
        for i in(0...v.count-1) {
            if [" ",".",",",":","/","-","=",";"].contains(v[i]) {
                processWord(word: word)
                word = String(v[i])
                processWord(word: word)
                word = ""
                continue
            }
            word+=v[i]
        }
        processWord(word: word)
        return result
        //
        func processWord(word:String) {
            if word == "" { return }
            let wordLength = stringWidth(word)
            if bufferLen+wordLength > columns {
                if result.count>0 {result = "\(result)\n" }
                result    = "\(result)\(buffer)"
                buffer    = ""
                bufferLen = 0
            }
            bufferLen += wordLength
            buffer    += word
        }
    }

wrapping is based on PIXEL width of strings, NOT character count

2 Likes

That‘s neat!