What's the best way to cut Swift string into 2-letter-strings?

前端 未结 2 1364
清酒与你
清酒与你 2020-12-21 19:09

I need to split a string into 2-letter pieces. Like “friend\" -> \"fr\" \"ie\" \"nd\". (Okay, its a step for me to change HEX string to Uint8 Array)

My code is

2条回答
  •  感动是毒
    2020-12-21 19:58

    Another option just for fun:

    extension String {
        var pairs:[String] {
            var result:[String] = []
            let chars = Array(characters)
            for index in 0.stride(to: chars.count, by: 2) {
                result.append(String(chars[index..

提交回复
热议问题