删除字符串前六个字符的最佳方法是什么?通过 Stack Overflow,我发现了几种本应是解决方案的方法,但我发现它们存在错误。例如,
extension String {
func removing(charactersOf string: String) -> String {
let characterSet = CharacterSet(charactersIn: string)
let components = self.components(separatedBy: characterSet)
return components.joined(separator: "")
}
如果我输入像 https://www.example.com
这样的网站,并将其存储为名为 website 的变量,然后输入以下内容
website.removing(charactersOf: "https://")
它会删除 https://
部分,但也会从文本中删除所有 h、所有 t、: 等。
我怎样才能只删除第一个字符?
最佳答案
在 Swift 4 中非常简单,只需使用 dropFirst(n: Int)
let myString = "Hello World"
myString.dropFirst(6)
//World
在你的情况下:website.dropFirst(6)
关于swift - 从字符串中删除前六个字符(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43383835/