替换出现的 "\:" [英] Replacing occurrences of "\:"

查看:49
本文介绍了替换出现的 "\:"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 \: 替换出现的 : 但我收到 文本中的转义序列无效 错误.我试过用 \\\: 转义,但它用双杠替换了出现:

I'm trying to replace occurrences of : with \: but I'm getting the Invalid escape sequence in literal error. I've tried escaping with \\\:, but it's replacing the occurrence with the double bar:

我需要:

2000:2018-07-25T10\:31\:06Z:2018-07-30

来自原始字符串:

2000:2018-07-25T10:31:06Z:2018-07-30

我得到:

2000:2018-07-25T10\\\:31\\\:06Z:2018-07-30

我的代码:

newString.replacingOccurrences(of: ":", with: "\:") //Invalid escape sequence in literal

newString.replacingOccurrences(of: ":", with: "\\:") //not quite want I need

推荐答案

您需要使用另一个反斜杠来转义反斜杠.所以如果你想用 "\:" 替换 ":" 你需要用 "\\:" 替换.

You need to escape backslashes with another backslash. So if you want to replace ":" with "\:"you need to replace with "\\:".

let backslashString = string.replacingOccurrences(of: ":", with: "\\:")

这篇关于替换出现的 "\:"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆