Swift Characters

  Total Views : 320
  Comments : 0
  Keywords : Swift character is used to represent single alphabet string literal with example, Swift character data type to represent characters with examples, Swift Unicode characters with examples

Here we will learn characters in swift with examples, how to use swift characters, characters concatenation in swift, string interpolation with character in swift, character overriding in swift, get characters from string with examples.

Swift Characters

In swift programming language 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 single literal for each variable and trying print all the characters using print() statement.

 

Following is the result of 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 character in swift programming language.

 

let a: Character = "S"

let b: Character = "D"

let ab = b

print(ab)

If you observe above example we are combining two characters “S” and “D” using 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 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 Character variable with String variable based on our requirements. 

 

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

 

var sname:  String = "Sures"

var uname:  Character = "h"

sname.append(uname)

print(sname)

If you observe above code we are concatenating string variable with character variable. Following is the result of 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 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. 

 

PREVIOUS

Swift Strings
 

NEXT

Swift Optionals  
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User