How to get the length of the last word in a string with Swift

1 Answer

0 votes
import Foundation

let str = "c++ c php javascript rust swift"

let words = str.split(separator: " ")

if let lastWord = words.last {
    print("The length of the last word is: \(lastWord.count)")
}



/*
run:

The length of the last word is: 5

*/

 



answered Jun 25, 2025 by avibootz
...