For every programming languages, characters and its combination, i.e., strings play a significant role in programming and bring with it some striking features that help programmers to shape messages and statements within the program. A string is a sequence of character. These strings and characters are used to display a message using println statement in Swift as you have seen earlier.


println("Hello w3")

println("The value is\(value1)")

You are familiar with these above println statements. The string written in double quotes is a sequence of string. In this chapter, you will mainly learn about character and strings and how they are used in Swift Programming.

What are The Characters in Swift?

Characters in swift are single String literal that are dealt with the data type character. Here is an example showing the way of declaring and initializing character variables:

import Cocoa

let ch1: Character = "G"

let ch2: Character = "S"

println ("First character is: \(ch1)")

println ("Second character is: \(ch2)")


First character is: G
Second character is: S

It is to be noted that Swift does not allow storing programmers' more than one value to a Character type variable. Moreover, it is also not possible to create an empty character variable or constant, which will have an empty value.

Characters From Strings

Programmers can also access every character from a string. As told above that string is a sequence of character, i.e., a collection of character value in a specific order. Therefore it is possible to access individual characters from a given string by letting it go with a particular time of iteration using a for-in loop. The program will look like this:

import Cocoa

for chr in "Welcome" {



What Are The Strings in Swift?

A String is an ordered collection of characters. The Character type is Unicode-compliant, so strings are also fully Unicode compliant. Empty string variables are declared as follows:

var str1: String

A Literal string value can also initialize strings:

var str1: String = "Welcome"

Like String literals, Character literals are enclosed in double quotes. Swift does not allow characters to be enclosed in single quotes like that in C language. Because Swift can infer types, it is not essential to include the String keyword when assigning a value. So programmers can also write the previous examples as follows:

var str = "Welcome"

var ch1: Character = "G"

Swift programmers can concatenate String types by using the + operator to create a new String. For example:

let newString = "Welcome" + "Karlos"

Programmers can also affix a String to an existing string by using the += operator. For Example:

var wc = "Welcome"

wc += "Karlos"

Since the string is a value type, so the instances are copied when assigned or passed to a function or method.

Comparison Operators

You can compare strings and substrings by using the below-listed comparison operators:

Operators Description
== Is equal to
!= Is not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to


import Cocoa
var var1   = "Hello, Swift!"
var var2   = "Hello, w3!"
if var1 == var2 {
   println( "\(var1) and \(var2) are equal" )
}else {
   println( "\(var1) and \(var2) are not equal" )

Concatenating Strings with Characters

import Cocoa
var var1:String = "Hi "
let var2:Character = "TC"
var1.append( var2 )
println("The value of var3  =  \(var1)")

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram