How to move the first word to the end of a string in Swift

1 Answer

0 votes
import Foundation  

func move_first_word_to_end_of_string(_ s: String) -> String {
    let parts = s.split(whereSeparator: { $0.isWhitespace }).map(String.init)

    if parts.count <= 1 {
        return s.trimmingCharacters(in: .whitespacesAndNewlines)
    }

    let first = parts[0]
    let rest = parts.dropFirst()

    return (rest + [first]).joined(separator: " ")
}

let s = "Would you like to know more? (Explore and learn)"
let result = move_first_word_to_end_of_string(s)

print(result)




/*
run:

you like to know more? (Explore and learn) Would

*/

 



answered Feb 5 by avibootz
...