Backspace (\b) Equivalent in swift language - ios

What is the \b equivalent in swift? I have to split a string which is received from server with \b?

From "Strings and Characters" in the Swift
Reference:
Special Characters in String Literals
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 Swift does not have a special character for the backspace
character, like \b in the C language. You can use the Unicode
special character \u{n}:
let string = "THIS\u{8}IS\u{8}A\u{8}TEST"
or create a string from the Unicode value:
let bs = String(UnicodeScalar(8))
let string = "THIS\(bs)IS\(bs)A\(bs)TEST"

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.

Find special characters entered in a textfield and escape in swift

My requirement is to create a JSON from the text entered in a UITextField. There is no restriction to the UITextField. So, if a user enters a special character(", \ etc.), I want to escape the value entered and create a JSON.
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
For example, if the user enters "Hello "User"! How to use a \ in a JSON?". It should return something like this "Hello \"User\"! How to use a \\ in a JSON?". Not just " or \, I would want to escape all the special characters.
Thanks! I truly appreciate your effort in providing me with a solution.
Edit
I forgot to mention, this requirement is for Swift 4.2.
Do not β€œmanually” escape the characters in order to create JSON. There is a dedicated JSONEncoder() class for this purpose.
Top-level JSON objects can only be arrays or dictionaries. Here is an example for an array containing a single element with the given string:
let text = """
Hello "User"! How to use a \\ in a JSON?
Another line line
"""
do {
let jsonData = try JSONEncoder().encode([text])
let jsonString = String(data: jsonData, encoding: .utf8)!
print(jsonString)
} catch {
print(error.localizedDescription)
}
The output is
["Hello \"User\"! How to use a \\ in a JSON?\nAnother line"]

swift: print \r character in debug

I use this text text\r2. And I want to print this in debug and get result:
text\r2
but I get this:
text
2
Try to escape the backslash with another backslash: text\\r2.
The \r will otherwise be interpreted as a line break.
\r in a String literal is a special character and represents a carriage return
See Special Characters in String Literals
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 quotation mark) and \' (single quotation mark)
* 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 (Unicode is discussed in Unicode below)
If you want to use in a String Literal a backslash you have to escape it using \\.
So you'll have to write
print("text\\r2")
to get text\r2

Swift regular expression invalid escape sequence in literal [duplicate]

I am trying to validate a phone number using NSPredicate and regex. The only problem is when setting the regex Swift thinks that I am trying to escape part of it due to the backslashes. How can I get around this?
My code is as follows:
let phoneRegEx = "^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$"
In Swift regular string literals, you need to double-escape the slashes to define literal backslashes:
let phoneRegEx = "^((\\(?0\\d{4}\\)?\\s?\\d{3}\\s?\\d{3})|(\\(?0\\d{3}\\)?\\s?\\d{3}\\s?\\d{4})|(\\(?0\\d{2}\\)?\\sβ€Œβ€‹?\\d{4}\\s?\\d{4}))(\\s?#(\\d{4}|\\d{3}))?$"
Starting from Swift 5, you can use raw string literals and escape regex escapes with a single backslash:
let phoneRegEx = #"^((\(?0\d{4}\)?\s?\d{3}\s?\d{3})|(\(?0\d{3}\)?\s?\d{3}\s?\d{4})|(\(?0\d{2}\)?\sβ€Œ?\d{4}\s?\d{4}))(\s?#(\d{4}|\d{3}))?$"#
Please refer to the Regular Expression Metacharacters table on the ICU Regular Expressions page to see what regex escapes should be escaped this way.
Please mind the difference between the regex escapes (in the above table) and string literal escape sequences used in the regular string literals that you may check, say, at Special Characters in String Literals:
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 quotation mark) and \' (single quotation mark)
An arbitrary Unicode scalar value, written as \u{n}, where n is a 1–8 digit hexadecimal number (Unicode is discussed in Unicode below)
So, in regular string literals, "\"" is a " string written as a string literal, and you do not have to escape a double quotation mark for the regex engine, so "\"" string literal regex pattern is enough to match a " char in a string. However, "\\\"", a string literal repesenting \" literal string will also match " char, although you can already see how redundant this regex pattern is. Also, "\n" (an LF symbol) matches a newline in the same way as "\\n" does, as "\n" is a literal representation of the newline char and "\\n" is a regex escape defined in the ICU regex escape table.
In raw string literals, \ is just a literal backslash.

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.

Resources