Character In Swift

Updated 29 November 2021

Save

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:

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.

Empty value not allowed

3. Swift doesn’t allow a single quote character.

Single Quote is not allowed

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:

  1. isASCII: Checks whether the character is a ASCII character
  2. isLetter: Checks whether the character is a letter
  3. asciiValue: Returns the ASCII encoded value of the character
  4. isWhiteSpace: Checks whether the character is a whitespace or newline.
  5. isNumber: Checks wheter the character represents a nubmer or not
  6. haxDigitValue: Returns the numeric value that this chracter represents
  7. description: Returns the textural representation of the character
  8. isHaxDigit: Checks whether the character represents a hex digit value.
  9. isCased: Checks whether the character changes in any form of case conversion.

However, there are a few methods available as well.

  1. uppercased(): It converts and returns the character into the upper case.
  2. 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.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home