ios Swift - 从字符串中删除 " 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25591241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:07:24  来源:igfitidea点击:

Swift - Remove " character from string

iosxcodestringswift

提问by Calan Williams

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

我有一个字符串,它是“可选(“5”)”。我需要删除 5 周围的“”。我通过执行以下操作删除了“可选”:

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.

我在删除 " 字符时遇到了困难,因为它们指定了代码中字符串的结尾。

回答by dasblinkenlight

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

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

  • \0(null character)
  • \\(backslash)
  • \t(horizontal tab)
  • \n(line feed)
  • \r(carriage return)
  • \"(double quote)
  • \'(single quote)
  • \0(空字符)
  • \\(反斜杠)
  • \t(水平标签)
  • \n(换行)
  • \r(回车)
  • \"(双引号)
  • \'(单引号)

This should work:

这应该有效:

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

回答by Alessandro Ornano

Swift 3 and Swift 4:

斯威夫特 3 和斯威夫特 4:

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

Latest documents updated to Swift 3.0.1 have:

更新到 Swift 3.0.1 的最新文档有:

  • Null Character (\0)
  • Backslash (\\)
  • Horizontal Tab (\t)
  • Line Feed (\n)
  • Carriage Return (\r)
  • Double Quote (\")
  • Single Quote (\')
  • Unicode scalar (\u{n}), where n is between one and eight hexadecimal digits
  • 空字符 ( \0)
  • 反斜杠 ( \\)
  • 水平标签 ( \t)
  • 换行 ( \n)
  • 回车 ( \r)
  • 双引号 ( \")
  • 单引号 ( \')
  • Unicode 标量 ( \u{n}),其中 n 介于 1 到 8 个十六进制数字之间

If you need more details you can take a look to the official docs here

如果您需要更多详细信息,可以在此处查看官方文档

回答by Chathuranga Silva

Here is the swift 3 updated answer

这是 swift 3 更新的答案

var editedText = myLabel.text?.replacingOccurrences(of: "\"", with: "")
Null Character (
Null Character (
println("\(text2!)")
) Backslash (\) Horizontal Tab (\t) Line Feed (\n) Carriage Return (\r) Double Quote (\") Single Quote (\') Unicode scalar (\u{n})
) Backslash (\) Horizontal Tab (\t) Line Feed (\n) Carriage Return (\r) Double Quote (\") Single Quote (\') Unicode scalar (\u{n})
var otherstring = "lat\" : 40.7127837,\n"
var new = otherstring.stringByTrimmingCharactersInSet(NSCharacterSet.init(charactersInString: "la t, \n \" ':"))
count(new) //result = 10
println(new) 
//yielding what I'm after just the numeric portion 40.7127837

回答by Norolim

To remove the optional you only should do this

要删除可选项,您只需执行此操作

if let realString = yourOriginalString {
    text2 = realString
} else {
    text2 = ""
}

cause if you dont use "!" it takes the optional value of text2

因为如果你不使用“!” 它采用 text2 的可选值

And to remove "" from 5 you have to convert it to NSInteger or NSNumber easy peasy. It has "" cause its an string.

并且要从 5 中删除 "",您必须将其转换为 NSInteger 或 NSNumber 容易的。它有“”,因为它是一个字符串。

回答by Getafix

I've eventually got this to work in the playground, having multiple characters I'm trying to remove from a string:

我最终让它在操场上工作,我试图从字符串中删除多个字符:

someString = someString.replacingOccurrences(of: "[abc]", with: "", options: [.regularExpression, .caseInsensitive])

回答by Dam

As Martin R says, your string "Optional("5")" looks like you did something wrong.

正如 Martin R 所说,你的字符串 "Optional("5")" 看起来你做错了什么。

dasblinkenlight answers you so it is fine, but for future readers, I will try to add alternative code as:

dasblinkenlight 回答你所以很好,但对于未来的读者,我会尝试添加替代代码:

var string = "potatoes + carrots"

text2 in your example looks like String and it is maybe already set to "" but it looks like you have an yourOriginalString of type Optional(String) somewhere that it wasn't cast or use correctly.

您的示例中的 text2 看起来像 String 并且它可能已经设置为 "" 但看起来您在某个地方有一个 Optional(String) 类型的 yourOriginalString ,它没有被正确转换或使用。

I hope this can help some reader.

我希望这可以帮助一些读者。

回答by Grzegorz R. Kulesza

If you want to remove more characters for example "a", "A", "b", "B", "c", "C" from string you can do it this way:

如果您想从字符串中删除更多字符,例如“a”、“A”、“b”、“B”、“c”、“C”,您可以这样做:

string = string.replacingOccurrences(of: "potatoes", with: "tomatoes", options: NSString.CompareOptions.literal, range: nil)

回答by Mini Official

Let's say you have a string:

假设您有一个字符串:

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

And you want to replace the word "potatoes" in that string with "tomatoes"

并且您想用“tomatoes”替换该字符串中的“potatoes”一词

string = string.replacingOccurrences(of: "potatoes", with: "dog\'s toys", options: NSString.CompareOptions.literal, range: nil)

If you print your string, it will now be: "tomatoes + carrots"

如果你打印你的字符串,它现在将是: "tomatoes + carrots"

If you want to remove the word potatoes from the sting altogether, you can use:

如果你想从刺中完全删除土豆这个词,你可以使用:

var text2: String = ""

If you want to use some other characters in your sting, use:

如果要在刺中使用其他一些字符,请使用:

  • Null Character (\0)
  • Backslash (\)
  • Horizontal Tab (\t)
  • Line Feed (\n)
  • Carriage Return (\r)
  • Double Quote (\")
  • Single Quote (\')
  • 空字符 (\0)
  • 反斜杠 (\)
  • 水平制表符 (\t)
  • 换行符 (\n)
  • 回车 (\r)
  • 双引号 (\")
  • 单引号 (\')

Example:

例子:

if let value = text {
    print(value)
}

Output: "dog's toys + carrots"

输出: "dog's toys + carrots"

回答by Ryan de Kwaadsteniet

You've instantiated text2 as an Optional (e.g. var text2: String?). This is why you receive Optional("5") in your string. take away the ? and replace with:

您已将 text2 实例化为 Optional(例如 var text2: String?)。这就是您在字符串中收到 Optional("5") 的原因。带走?并替换为:

##代码##

回答by Ale Mohamad

If you are getting the output Optional(5)when trying to print the value of 5in an optional Intor String, you should unwrap the value first:

如果您Optional(5)在尝试打印5可选IntString 中的值时获得输出,则应首先解开该值:

##代码##

Now you've got the value without the "Optional" string that Swift adds when the value is not unwrapped before.

现在你已经得到了没有“可选”字符串的值,当值之前没有解包时,Swift 会添加这个字符串。