In this tutorial you will learn about the Swift Characters and its application with practical example.
Swift Characters
A character is used to represents a single literal, it could an alphabet, number or any special symbol.In Swift, character data type is used to hold a character value. Character can also be a languages character other than English using escape sequence \u{n} (unicode code point ,where n is in hexadecimal)
Declaring character
In Swift, character is declared same as string using double quotes.
Syntax:-
1 |
let <char_var_name>:Character = "<any_single_char>" |
Example:-
1 2 |
let chr:Character = "K" print(chr) |
Loop through characters from string
In Swift, string is a collection of characters thus it is possible to loop through characters from given string using for-in loop as following –
Example:-
1 2 3 4 |
let site = "W3Adda" for chr in site { print(chr) } |
Output:-