How do I take a substring to the first index of a character?

后端 未结 3 1302
予麋鹿
予麋鹿 2021-01-24 12:42

I\'m very new to Swift; I\'ve spent the morning reading StackOverflow and trying many strategies, in vain, to accomplish the following:

I have a string, say \"12345 is

3条回答
  •  再見小時候
    2021-01-24 12:51

    With Swift 5 you can use:

    myStr.prefix(upTo: myStr.firstIndex(of: " ") ?? myStr.startIndex)
    

    You may need to cast it back to String (String(myStr.prefix(upTo: myStr.firstIndex(of: " ") ?? myStr.startIndex))) since it returns a Substring

提交回复
热议问题