10

I'm sending regular expressions to CloudKit as a String value and it doesn't seem to like it, replacing \\by \. However, once I'm getting this value from my app I would like to retransform it in its original form, with \\instead of \.

I don't know how to manage this kind of escaped characters in Swift because I cannot even set a String with a \ in my code but I'm still able to manage them when getting them from CloudKit. Here is an example of String:

var onlyOneBackslash: String = valueFromCloudKit
print(onlyOneBackslash) // booking\.com

How to escape the backslash to transform booking\.com into booking\\.com?

Armand Grillet
  • 3,229
  • 5
  • 30
  • 60

2 Answers2

13

The double backslash exists only in your code, it is a convention of the compiler. It never exists in the string itself, just in the Swift code.

If you want a double backslash in the string you need to have four backslashes in your code. Or use a String method to replace single backslashes with double backslashes.

Code example:

let originalString = "1\\2"
print("originalString: \(originalString)")
let newString = originalString.stringByReplacingOccurrencesOfString("\\", withString: "\\\\", options: .LiteralSearch, range: nil)
print("newString: \(newString)")

Output:

originalString: 1\2  
newString: 1\\2  
zaph
  • 111,848
  • 21
  • 189
  • 228
  • What to do if you want the "1\2" value to a string variable ? – Shahid Aslam Mar 13 '16 at 09:19
  • If you need to include a "\" in a string just escape it "\\". See [Strings and Characters](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html#//apple_ref/doc/uid/TP40014097-CH7-ID285) and scroll to *Special Characters in String Literals*. – zaph Mar 13 '16 at 11:53
  • 1
    Thanks for the reply Zaph, actually the problem is I wanted to send "\/Date(1310669017000)\/" as a parameter to some web-service if i escape this with \\ it will send that parameter as "\\/Date(1310669017000)\\/" not "\/Date(1310669017000)\/". I thing escape only works with print(). – Shahid Aslam Mar 14 '16 at 09:56
  • There are two things here. 1. putting a "\" in a string, several characters need escaping when created as literals in code. 2. URL encoding certain characters for URLs. There can be some overlap. For URL encoding see this [SO answer](http://stackoverflow.com/questions/24551816/swift-encode-url/24552028#24552028). – zaph Mar 14 '16 at 11:31
  • Actually i have to send "\/Date(1310669017000)\/" parameter as POST i am not encoding this in a URL. – Shahid Aslam Mar 15 '16 at 09:44
  • @ShahidAslam Do you get any solution for the problem I am also doing the same and can't find any solution – Swapnil Dhotre Mar 02 '17 at 19:37
1

Swift 5 version

let originalString = "1\\2"
print("originalString: \(originalString)")
let newString = originalString.replacingOccurrences(of: "\\", with: "\\\\")
print("newString: \(newString)")
replacingOccurrences(of: "\\", with: "\\\\")

Output:

originalString: 1\2  
newString: 1\\2  
Mike Zriel
  • 1,575
  • 1
  • 17
  • 28