使用Swift从yaml块中删除行

时间:2017-11-04 01:44:02

标签: swift yaml

我的目标是从字符串变量中删除Yaml键和值,但是我失败了。

MWE

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: "")

问题

  • 以上操作不起作用,即不从yaml块中删除电子邮件行,如何删除包含作者和电子邮件信息的整行?

1 个答案:

答案 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

请注意作者行,您的目标不包括,但您的代码和问题也没有说明您要删除。希望这有帮助!