The Character type represents a character instance that is composed of one or more Unicode Scalar Values. These Unicode scalar values are grouped together as an extended grapheme cluster by using the Unicode boundary algorithm. Swift uses Character to represent string literals. Below is an example of this:
1 2 3 |
var c: Character = "a" print("\(c) type is of", type(of: c)) |
Key Points About Character
Now, there are some key points that we have to remember about Character data type before using it in our programs. These points are as follows:
1. A character variable can store only one character.
2. Swift does not allow character variables to store an empty value.
3. Swift doesn’t allow a single quote character.
4. Character
keyword is required at the time of declaring a character variable else the variable will become string type.
Above all, are the important point which you need to keep in mind while working with character types. Now, we will look at how can we access characters from a string.
Accessing Character from a String variable
We can access characters from a string by iterating over a string variable.
We can also perform character manipulation on the string by accessing these variables. Similarly, by using the description
property we can get the string representation of the character. However, we can also fetch the ASCII value of a character by using asciiValue
property if available.
Character Properties and Methods
Few Properties of Character struct are as follows:
isASCII
: Checks whether the character is a ASCII characterisLetter
: Checks whether the character is a letterasciiValue
: Returns the ASCII encoded value of the characterisWhiteSpace
: Checks whether the character is a whitespace or newline.isNumber
: Checks wheter the character represents a nubmer or nothaxDigitValue
: Returns the numeric value that this chracter representsdescription
: Returns the textural representation of the characterisHaxDigit
: Checks whether the character represents a hex digit value.isCased
: Checks whether the character changes in any form of case conversion.
However, there are a few methods available as well.
uppercased()
: It converts and returns the character into the upper case.lowercased()
: It converts and returns the character into the lower case.
Thank you for reading this article. If you want to know more about Character the click here. After that, if you want to read more articles regarding iOS Development click here.