Swift 3.1 如何删掉一个字符串里的部分指定的字符串?

在Swift 3.1 中,想将 “/abc/def/ghi”中的“/abc”删掉,有什么方法可以做?

用substring(from: "/abc".endIndex), 会出现编译错误:

"__TFE10FoundationSS9substringfT4fromVVSS13CharacterView5Index_SS", referenced from:

不懂!?

阅读 6.6k
1 个回答

可以使用下面的方法:

str.replacingOccurrences(of: "/ abc", with: "")

参考文档:

func replacingOccurrences(of:with:options:range:)

[Foundation] Returns a new string in which all occurrences of a target string in a specified range of the String are replaced by another given string.

Declaration

func replacingOccurrences(of target: String, with replacement: String, options: CompareOptions = default, range searchRange: Range<Index>? = default) -> String

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进