斯威夫特 - 删除“字符串中的字符 [英] Swift - Remove " character from string

查看:127
本文介绍了斯威夫特 - 删除“字符串中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,它是Optional(5)。我需要删除5周围的。我已经删除了'可选':

I have a string which is "Optional("5")". I need to remove the "" surrounding the 5. I have removed the 'Optional' by doing:

text2 = text2.stringByReplacingOccurrencesOfString("Optional(", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)

我无法删除字符,因为它们在代码中指定字符串的结尾。

I am having difficulties removing the " characters as they designate the end of a string in the code.

推荐答案

Swift使用反斜杠来逃避双引号。这是Swift中转义的特殊字符列表:

Swift uses backslash to escape double quotes. Here is the list of escaped special characters in Swift:



  • \\ \\ 0 (null character)

  • \\ (反斜杠)

  • \t (水平标签)

  • \ n (换行)

  • \ r (回车)

  • \(双引号)

  • \'(单引号)

  • \0 (null character)
  • \\ (backslash)
  • \t (horizontal tab)
  • \n (line feed)
  • \r (carriage return)
  • \" (double quote)
  • \' (single quote)

这应该有效:

text2 = text2.replacingOccurrences(of: "\\", with: "", options: NSString.CompareOptions.literal, range: nil)

这篇关于斯威夫特 - 删除“字符串中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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