swift - 从字符串中删除前六个字符(Swift)

标签 swift string character

删除字符串前六个字符的最佳方法是什么?通过 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/

相关文章:

swift - 跨集合扩展 customstringconvertible

ios - 无法将数据从一个 View Controller 推送到 TableView Controller

php - 获取 HTML 标签之间的文本

c - 如何使用 C 将一个 long 值(32 位)拆分为四个 char 变量(8 位)?

php - sphinx 搜索 : charset table difficulties

ios - 父 View 中的状态更改创建子 ViewModel 的新实例

python - 在拆分字符串列表中查找字符串

Javascript,有没有办法将数组编写为没有逗号或空格的单个文本字符串?

c - 如何将一个int转换为一系列字符

ios - 在 swift 中,为什么我必须使用 IBaction 或 IBOutlet 在代码和 UI 之间进行通信?