
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Demonstrate Escape Sequence Characters in Swift
In Swift, escape sequence characters are those characters who do not represent themselves when they are used inside a string. Instead, they tell the compiler to perform the specified task. They are non-printable characters. Swift supports the following escape sequence characters in the string ?
Escape Sequence Characters |
Description |
---|---|
\n |
It is a newline character. It tells the compiler to move to the new line. |
\t |
It is known as the horizontal tab. It tells the compiler to leave the tab. |
\r |
It is known as carriage return. It tells the compiler to move the cursor to the starting of the line. |
" |
It is known as the double quotation mark. It tells the compiler to warp the string with double quotes. |
' |
It is known as a single quotation mark. It tells the compiler to warp the string with single quote. |
\0 |
It is a null character. It tells the compiler to terminate the string at a certain context. |
\ |
It is a backslash. It tells the compiler to add backslash to the string. |
\u{X} |
It is an arbitrary unicode scalar value, where X is a 1-8 digit hexadecimal number. It allows you to add characters from other unicode character sets. |
These escape sequence characters are used inside the string as shown in the below syntax ?
Let str = "Rainbow is "very \beautiful"
Example 1
In the following Swift program, we will demonstrate the escape sequence characters.
import Foundation import Glibc // Using newline character let myStr1 = "Birds are playing \nin the rain" print("String 1:", myStr1) // Using horizontal tab character let myStr2 = "Ram is good\tIOS developer" print("String 2:", myStr2) // Using carriage return character let myStr3 = "Sita loves \rpizza" print("String 3:", myStr3) // Using double quotation mark character let myStr4 = "Rohan like \"Swift programming\"" print("String 4:", myStr4)
Output
String 1: Birds are playing in the rain String 2: Ram is good IOS developer pizzag 3: Sita loves String 4: Rohan like "Swift programming"
Example 2
In the following Swift program, we will demonstrate the escape sequence characters.
import Foundation import Glibc // Using single quotation mark character let myStr1 = "Kids are going to \'school\'" print("String 1:", myStr1) // Using null character let myStr2 = "Develop \0swift program" print("String 2:", myStr2) // Using backslash let myStr3 = "Rohan play \\ football" print("String 3:", myStr3) // Using arbitrary unicode scalar value let myStr4 = "\u{24}" print("String 4:", myStr4)
Output
String 1: Kids are going to 'school' String 2: Develop swift program String 3: Rohan play \ football String 4: $
Conclusion
So this is how we can use escape sequence characters. They allow you to represent some specific characters that are very hard to directly type in a string. Or we can say that they are special characters with a special task. Suppose you want to print single quotes but in a string, you can not directly print single quotes because the compiler thinks that the given single quotes are part of the syntax, so using ' character you can achieve your goal.