Swift - Remove from row

I have a line that is "Optional (" 5 "). I need to delete "around 5. I removed" Optional "by doing:

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

I'm having difficulty deleting characters as they mark the end of a line in the code.

+62
string ios xcode swift
Aug 31 '14 at 10:50
source share
10 answers

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

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

This should work:

 text2 = text2.replacingOccurrences(of: "\\", with: "", options: NSString.CompareOptions.literal, range: nil) 
+138
Aug 31 '14 at 10:55
source share

Swift 3 and Swift 4:

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

Recent documents updated to Swift 3.0.1 have:

  • Zero character ( \0 )
  • Backslash ( \\ )
  • Horizontal tab ( \t )
  • Line feed ( \n )
  • Carriage Return ( \r )
  • Double quote ( \" )
  • Single quote ( \' )
  • Unicode scalar ( \u{n} ), where n is from one to eight hexadecimal digits

If you need more information, you can see the official docs here

+46
Oct 26 '16 at 10:17
source share

Here is a quick 3 updated answer

 var editedText = myLabel.text?.replacingOccurrences(of: "\"", with: "") 
 Null Character (\0) Backslash (\\) Horizontal Tab (\t) Line Feed (\n) Carriage Return (\r) Double Quote (\") Single Quote (\') Unicode scalar (\u{n}) 
+20
Jun 15 '17 at 6:33
source share

To remove the optional you must do this

 println("\(text2!)") 

reason if you do not use "!" it takes an optional value text2

And to remove "from 5, you need to convert it to NSInteger or NSNumber easy peasy. It has a" ", calling its string.

+9
Jan 05 '15 at 13:01
source share

In the end, I got this to work on the playground, having a few characters that I'm trying to remove from a string:

 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 
+6
Jul 23 '15 at 5:34
source share

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

dasblinkenlight is responding to you, so this is fine, but for future readers I will try to add alternative code:

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

text2 in your example looks like a String, and it may already be set to "", but it looks like you have yourOriginalString type of type Optional (String) somewhere that it has not been deprived or used correctly.

Hope this helps some readers.

+4
Dec 15 '14 at 10:37
source share

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

 someString = someString.replacingOccurrences(of: "[abc]", with: "", options: [.regularExpression, .caseInsensitive]) 
+1
Jan 14 '19 at 9:35
source share

You created an instance of text2 as optional (e.g. var text2: String?). This is why you get Optional ("5") on your line. pick up? and replace with:

 var text2: String = "" 
0
Apr 24 '17 at 18:00
source share

Let's say you have a line:

 var string = "potatoes + carrots" 

And you want to replace the word "potato" in this line with "tomatoes"

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

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

If you want to completely remove the word "potato" from a bite, you can use:

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

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

  • Zero character (\ 0)
  • Backslash (\)
  • Horizontal tab (\ t)
  • Line feed (\ n)
  • Carriage Return (\ r)
  • Double quote (\ ")
  • Single quote (\ ')

Example:

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

Conclusion: "dog toys + carrots"

0
Aug 30 '18 at 8:10
source share

If you get Optional(5) output when you try to print the value 5 in an optional Int or String , you must first expand the value:

 if let value = text { print(value) } 

You now have a value without the โ€œOptionalโ€ line that Swift adds when the value has not been expanded before.

0
May 20 '19 at 3:33
source share



All Articles