Swift Characters

In swift, Character represents a single alphabet or literal and by using Character data type we can define characters.

 

Generally in any programming language string can be formed by combining multiple characters.

 

Following is the simple example of representing characters in swift programming language.

 

var sname:  Character = "S"

var uname:  Character = "u"

var rname:  Character = "r"

var ename:  Character = "e"

var ssname:  Character = "s"

var hname:  Character = "h"

 

print(sname,uname,rname,ename,ssname,hname)

If you observe above example we defined a single literal for each variable and trying print all the characters using print() statement.

 

Following is the result of an above swift program to print characters.

 

S u r e s h

In case if we try to store more than one character in Character data type then the Xcode Editor gives an error and give suggestion to convert into String datatype because Character data type allows only one character at a time like as shown below.

 

Swift throwing error when we try to add more than one character to character variable

Swift Characters Concatenation

In swift we can easily concatenate two characters but more than two characters concatenation is not allowed in swift. 

 

Following is the simple example of concatenating two characters in a swift programming language.

 

let a: Character = "S"

let b: Character = "D"

let ab = b

print(ab)

If you observe the above example we are combining two characters “S” and “D” using the addition operator.

 

When we run above program swift playground will return the result like as shown below.

 

DD

As we discussed earlier swift allows only two characters concatenation but if we try to combine more than 2 characters then we will get an error like as shown below.

 

Swift throws error when we try to combine more than two character variables

Swift Character Concatenation with String

In Swift, we can concatenate the Character variable with a String variable based on our requirements. 

 

Following is the simple example of concatenating Character with String in a swift programming language.

 

var sname:  String = "Sures"

var uname:  Character = "h"

sname.append(uname)

print(sname)

If you observe the above code we are concatenating string variable with character variable. Following is the result of the above swift program.

 

Suresh

Swift Get Characters from String

If we want to get or access individual characters from a string then we need to loop through string over its characters property by using the for-in loop.

 

Following is the simple example to loop through over string to get individual character from string.

 

let name = "Suresh"

for i in name.characters {

print(i)

}

If you observe above example we are looping through string called “name” using for-in loop over its characters property.

 

When we run above program swift playground will return data like as shown below.

 

S

u

r

e

s

h

Swift Character Overriding

In swift we can override the characters based on our requirements. Following is the simple example of overriding characters in swift programming language.

 

var a: Character = "S"

let b: Character = "D"

b

var result = b

print(result)

If you observe above example we are overriding value “a” with value of “b”.

 

When we run above program swift playground will return the result like as shown below.

 

DD

This is how we can use characters in swift programming language to concatenate strings, character overriding and to get characters from string, etc. based on our requirements.