Extract Last Word in String with Swift

后端 未结 6 1047
傲寒
傲寒 2020-12-09 12:13

What is the way of extracting last word in a String in Swift? So if I have \"Lorem ipsum dolor sit amet\", return \"amet\". What is the most efficient way of doing this?

6条回答
  •  暖寄归人
    2020-12-09 12:24

    First use componentsSeparatedByString to split your string into an array by space, then get the last word of the array using .last

    var string = "Lorem ipsum dolor sit amet"
    var stringArr = string.componentsSeparatedByString(" ")
    
    var lastWord = stringArr.last //amet
    

提交回复
热议问题