我的目标是从字符串变量中删除Yaml键和值,但是我失败了。
let testMe = """
---
# Metadata
title: hum
author: jecatatu
email: jecatatu@gmail.com
---
This is more text outside the yaml block
"""
目标是有一个字符串:
let testMe = """
---
# Metadata
title: hum
---
This is more text outside the yaml block
"""
到目前为止我的代码看起来像:
var email = "jecatatu@gmail.com"
let emailline = "email: " + email
let document:String = testMe.replacingOccurrences(of: emailline, with: "")
答案 0 :(得分:1)
如果您将以下内容放入Swift 4游乐场:
import UIKit
var testMe = """
---
# Metadata
title: hum
author: jecatatu
email: jecatatu@gmail.com
---
This is more text outside the yaml block
"""
var email = "jecatatu@gmail.com"
let emailline = "email: " + email
testMe = testMe.replacingOccurrences(of: "\n\(emailline)", with: "")
print(testMe)
您将获得以下testMe版本:
---
# Metadata
title: hum
author: jecatatu
---
This is more text outside the yaml block
请注意作者行,您的目标不包括,但您的代码和问题也没有说明您要删除。希望这有帮助!