【问题标题】:Get substring till end of string index from string从字符串获取子字符串直到字符串索引的结尾
【发布时间】:2019-01-28 06:38:15
【问题描述】:

我是 swift 新手。

我需要从字符串中获取子字符串,直到字符串的结束索引

例如:

我的字符串是 Hello Playground 你好吗?

如果我的子字符串包含“how”,它应该返回“你好吗?”

如果我的子字符串包含“Hello”。它应该返回“Hello Playground 你好吗?”

如果我的子字符串包含“playground”,它应该返回“Playground 你好吗?”

我可以使用 contains 方法查看子字符串是否存在

如果是,那么我如何获得该子字符串的索引让我感到困惑

如果我可以得到子字符串的索引,我可以使用类似的东西

让 mySubstringString = str[substring.startIndex..

请指教

【问题讨论】:

标签: swift string substring


【解决方案1】:

你可以试试这个,

let string = "Hello Playground how are you?"
let sub = "Playground"
if let startIndex = string.range(of: sub)?.lowerBound {
    let newString = string[startIndex..<string.endIndex]
    print(newString) // Playground how are you?
}

【讨论】:

    猜你喜欢
    • 2016-01-01
    • 2013-01-28
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多