Reverse words in a sentence - Swift

后端 未结 5 842
不知归路
不知归路 2021-01-29 15:43

How to perform reversing of all words in a sentence.

Example

let str = \"Hello playground\"

Result should be like \"olleH dnuorg

5条回答
  •  不思量自难忘°
    2021-01-29 16:23

    let result = String(str.reversed())
        .components(separatedBy: .whitespaces)
        .reversed()
        .joined(separator: " ")
    

提交回复
热议问题