Swift - remove single backslash - ios

this is maybe stupid question but I'm new to swift and i actually can't figure this out.
I have API which returns url as string "http:\/\/xxx". I don't know how to store URL returned from API in this format. I can't store it to variable because of backslash.
From apple doc:
...string cannot contain an unescaped backslash (\), ...
Is there any way how to store string like this or how remove these single backslashes or how to work with this?
Thank you for every advice.

You can just replace those backslashes, for example:
let string2 = string1.stringByReplacingOccurrencesOfString("\\", withString: "")
Or, to avoid the confusion over the fact that the backslash within a normal string literal is escaped with yet another backslash, we can use an extended string delimiter of #" and "#:
let string2 = string1.stringByReplacingOccurrencesOfString(#"\"#, withString: "")
But, if possible, you really should fix that API that is returning those backslashes, as that's obviously incorrect. The author of that code was apparently under the mistaken impression that forward slashes must be escaped, but this is not true.
Bottom line, the API should be fixed to not insert these backslashes, but until that's remedied, you can use the above to remove any backslashes that may occur.
In the discussion in the comments below, there seems to be enormous confusion about backslashes in strings. So, let's step back for a second and discuss "string literals". As the documentation says, a string literal is:
You can include predefined String values within your code as string literals. A string literal is a fixed sequence of textual characters surrounded by a pair of double quotes ("").
Note, a string literal is just a representation of a particular fixed sequence of characters in your code. But, this should not be confused with the underlying String object itself. The key difference between a string literal and the underlying String object is that a string literal allows one to use a backslash as an "escape" character, used when representing special characters (or doing string interpolation). As the documentation says:
String literals can include the following special characters:
The escaped special characters \0 (null character), \\ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)
An arbitrary Unicode scalar, written as \u{n}, where n is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point
So, you are correct that in a string literal, as the excerpt you quoted above points out, you cannot have an unescaped backslash. Thus, whenever you want to represent a single backslash in a string literal, you represent that with a \\.
Thus the above stringByReplacingOccurrencesOfString means "look through the string1, find all occurrences of a single backslash, and replace them with an empty string (i.e. remove the backslash)."
Consider:
let string1 = "foo\\bar"
print(string1) // this will print "foo\bar"
print(string1.characters.count) // this will print "7", not "8"
let string2 = string1.stringByReplacingOccurrencesOfString("\\", withString: "")
print(string2) // this will print "foobar"
print(string2.characters.count) // this will print "6"
A little confusingly, if you look at string1 in the "Variables" view of the "Debug" panel or within playground, it will show a string literal representation (i.e. backslashes will appear as "\\"). But don't be confused. When you see \\ in the string literal, there is actually only a single backslash within the actual string. But if you print the value or look at the actual characters, there is only a single backslash in the string, itself.
In short, do not conflate the escaping of the backslash within a string literal (for example, the parameters to stringByReplacingOccurrencesOfString) and the single backslash that exists in the underlying string.

I found I was having this same issue when trying to encode my objects to JSON. Depending on if you're using the newer JSONEncoder class to parse your JSON and you're supporting a minimum of iOS 13, you can use the .withoutEscapingSlashes output formatting:
let encoder = JSONEncoder()
encoder.outputFormatting = .withoutEscapingSlashes
try encoder.encode(yourJSONObject)

Please check the below code.
let jsonStr = "[{\"isSelected\":true,\"languageProficiencies\":[{\"isSelected\":true,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"English\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Malay\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Chinese\"},{\"isSelected\":false,\"languageProficiencies\":[{\"isSelected\":false,\"name\":\"Advance\"},{\"isSelected\":false,\"name\":\"Proficient\"},{\"isSelected\":false,\"name\":\"Basic\"},{\"isSelected\":false,\"name\":\"Below Basic\"}],\"name\":\"Tamil\"}]"
let convertedStr = jsonStr.replacingOccurrences(of: "\\", with: "", options: .literal, range: nil)
print(convertedStr)

I've solved with this piece of code:
let convertedStr = jsonString.replacingOccurrences(of: "\\/", with: "/")

To remove single backslash,try this
let replaceStr = backslashString.replacingOccurrences(of: "\"", with: "")

Include a backslash in a string by adding an extra backslash.

Related

I'm trying to remove backslash from string but if I print out with print I get the correct string , but if I print it with "po" I get the same string

MyString = "CfegoAsZEM/sP\u{10}\u{10}}"
MyString.replacingOccurrences(of: "\"", with: "")
with print(MyString) I got this : "CfegoAsZEM/sP" (that's what I need)
with po MyString (on the debugger) : "CfegoAsZEM/sP\u{10}\u{10}}"
\u{10} is a linefeed character
Maybe a better way is to trim the string, it removes all whitespace and newline characters from the beginning and the end of the string
let myString = "CfegoAsZEM/sP\u{10}\u{10}"
let trimmedString = myString.trimmingCharacters(in: .whitespacesAndNewlines)
Your string doesn't contain literal backslash characters. Rather, the \u{} sequence is an escaped sequence that introduces a Unicode character. This is why you can't remove it using replacingOccurrences.
In this case, as Vadian pointed out it is the "new line" character (0x10). Since this is an invisible "white space" character you don't see it when you print the string, but you do see it when you use po. The debugger shows you escape sequences for non-printable characters. You will also see the sequence if you print(MyString.debugDescription)
Unfortunately the trimmingCharactersIn function doesn't appear to consider Unicode sequences.
We can use the filter function to examine each character in the string. If the character is ASCII and has a value greater than 31 ( 32 is the space character, the first "printable" character in the ASCII sequence) we can include it. We also need to ensure that values that aren't ASCII are included so as not to strip printable Unicode characters (e.g. emoji or non-Latin characters).
let MyString = "CfegoAsZEM/sP\u{10}\u{13}$}πŸ”…\u{1F600}".filter { $0.asciiValue ?? 32 > 31 }
print(MyString.debugDescription)
print(MyString)
Output
"CfegoAsZEM/sP}πŸ”…πŸ˜€"
CfegoAsZEM/sP}πŸ”…πŸ˜€
asciiValue returns an optional, which is nil if the character isn't plain ASCII. I have used a nil-coalescing operator to return 32 in this case so that the character isn't filtered.
I modified the initial string to include some printable Unicode to demonstrate that it isn't stripped by the filter.

How to send an HTML email to the user when a button is pressed? (Swift) [duplicate]

Can someone please tell me how can I print something in following way "with" double quotes.
"Double Quotes"
With a backslash before the double quote you want to insert in the String:
let sentence = "They said \"It's okay\", didn't they?"
Now sentence is:
They said "It's okay", didn't they?
It's called "escaping" a character: you're using its literal value, it will not be interpreted.
With Swift 4 you can alternatively choose to use the """ delimiter for literal text where there's no need to escape:
let sentence = """
They said "It's okay", didn't they?
Yes, "okay" is what they said.
"""
This gives:
They said "It's okay", didn't they?
Yes, "okay" is what they said.
With Swift 5 you can use enhanced delimiters:
String literals can now be expressed using enhanced delimiters. A string literal with one or more number signs (#) before the opening quote treats backslashes and double-quote characters as literal unless they’re followed by the same number of number signs. Use enhanced delimiters to avoid cluttering string literals that contain many double-quote or backslash characters with extra escapes.
Your string now can be represented as:
let sentence = #"They said "It's okay", didn't they?"#
And if you want add variable to your string you should also add # after backslash:
let sentence = #"My "homepage" is \#(url)"#
For completeness, from Apple docs:
String literals can include the following special characters:
The escaped special characters \0 (null character), \ (backslash), \t
(horizontal tab), \n (line feed), \r (carriage return), \" (double
quote) and \' (single quote)
An arbitrary Unicode scalar, written as
\u{n}, where n is a 1–8 digit hexadecimal number with a value equal to
a valid Unicode code point
which means that apart from being able to escape the character with backslash, you can use the unicode value. Following two statements are equivalent:
let myString = "I love \"unnecessary\" quotation marks"
let myString = "I love \u{22}unnecessary\u{22} quotation marks"
myString would now contain:
I love "unnecessary" quotation marks
According to your needs, you may use one of the 4 following patterns in order to print a Swift String that contains double quotes in it.
1. Using escaped double quotation marks
String literals can include special characters such as \":
let string = "A string with \"double quotes\" in it."
print(string) //prints: A string with "double quotes" in it.
2. Using Unicode scalars
String literals can include Unicode scalar value written as \u{n}:
let string = "A string with \u{22}double quotes\u{22} in it."
print(string) //prints: A string with "double quotes" in it.
3. Using multiline string literals (requires Swift 4)
The The Swift Programming Language / Strings and Characters states:
Because multiline string literals use three double quotation marks instead of just one, you can include a double quotation mark (") inside of a multiline string literal without escaping it.
let string = """
A string with "double quotes" in it.
"""
print(string) //prints: A string with "double quotes" in it.
4. Using raw string literals (requires Swift 5)
The The Swift Programming Language / Strings and Characters states:
You can place a string literal within extended delimiters to include special characters in a string without invoking their effect. You place your string within quotation marks (") and surround that with number signs (#). For example, printing the string literal #"Line 1\nLine 2"# prints the line feed escape sequence (\n) rather than printing the string across two lines.
let string = #"A string with "double quotes" in it."#
print(string) //prints: A string with "double quotes" in it.

SWIFT string with special characters without escape

How to print all special characters without inserting escape sign before every of them? I have very large textiles with many special characters and I'm looking for something like # in c# which prints string literally as it is
What you're referring to, is called a verbatim string literal in C# and that concept does not translate exactly to Swift.
However, with the introduction of multiline string Literals in Swift 4, you can get close.
let multilineString = """
Here you can use \ and newline characters.
Also single " or double "" are allowed.
"""
For reference, find the grammar of a Swift String literal here.

Removing single backslash from string

I am getting a string for a place name back from an API: "Moe\'s Restaurant & Brewhouse". I want to just have it be "Moe's Restaurant & Brewhouse" but I can't get it to properly format without the \.
I've seen the other posts on this topic, I've tried placeName?.stringByReplacingOccurrencesOfString("\\", withString: "") and placeName?.stringByReplacingOccurrencesOfString("\'", withString: "'"). I just can't get anything to work. Any ideas so I can get the string how I want it without the \? Any help is greatly appreciated, thanks!!
You report that the API is returning "Moe\'s Restaurant & Brewhouse". More than likely you are looking at a Swift dictionary or something like that and it is showing you the string literal representation of that string. But depending upon how you're printing that, the string most likely does not contain any backslash.
Consider the following:
let string = "Moe's"
let dictionary = ["name": string]
print(dictionary)
That will print:
["name": "Moe\'s"]
It is just showing the "string literal" representation. As the documentation says:
String literals can include the following special characters:
The escaped special characters \0 (null character), \\ (backslash), \t (horizontal tab), \n (line feed), \r (carriage return), \" (double quote) and \' (single quote)
An arbitrary Unicode scalar, written as \u{n}, where n is a 1–8 digit hexadecimal number with a value equal to a valid Unicode code point
But, note, that backslash before the ' in Moe\'s is not part of the string, but rather just an artifact of printing a string literal with an escapable character in it.
If you do:
let string2 = dictionary["name"]!
print(string2)
It will show you that there is actually no backslash there:
Moe's
Likewise, if you check the number of characters:
print(dictionary["name"]!.characters.count)
It will correctly report that there are only five characters, not six.
(For what it's worth, I think Apple has made this far more confusing than is necessary because it sometimes prints strings as if they were string literals with backslashes, and other times as the true underlying string. And to add to the confusion, the single quote character can be escaped in a string literal, but doesn't have to be.)
Note, if your string really did have a backslash in it, you are correct that this is the correct way to remove it:
someString.stringByReplacingOccurrencesOfString("\\", withString: "")
But in this case, I suspect that the backslash that you are seeing is an artifact of how you're displaying it rather than an actual backslash in the underlying string.

(Swift) how to print "\" character in a string?

I have tried to print it but it just by passes because it's an escaped character.
e.g output should be as follows.
\correct
For that and also future reference:
\0 – Null character (that is a zero after the slash)
\\ – Backslash itself. Since the backslash is used to escape other characters, it needs a special escape to actually print itself.
\t – Horizontal tab
\n – Line Feed
\r – Carriage Return
\” – Double quote. Since the quotes denote a String literal, this is necessary if you actually want to print one.
\’ – Single Quote. Similar reason to above.
Use the following code for Swift 5, Xcode 10.2
let myText = #"This is a Backslash: \"#
print(myText)
Output:
This is a Backslash: \
Now not required to add a double slash to use a single slash in swift 5, even now required slash before some character, for example, single quote, double quote etc.
See this post for latest update about swift 5
https://www.hackingwithswift.com/articles/126/whats-new-in-swift-5-0
var s1: String = "I love my "
let s2: String = "country"
s1 += "\"\(s2)\""
print(s1)
It will print I love my "country"
The backslash character \ acts as an escape character when used in a string. This means you can use, for example, double quotes, in a string by pre-pending them with \. The same also applies for the backslash character itself, which is to say that println("\\") will result in just \ being printed.

Resources