Swift Characters and Strings

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

Example:
println("Hello w3")

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

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

What are 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)")
Output:
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. More over 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 each and 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 specific time of iteration using a for-in loop. The program will look like this:

import Cocoa

for chr in "Welcome" {
   println(chr)
}
Output:
w
e
l
c
o
m
e

What are 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

Strings can also be initialized by a string Literal value:

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 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:

OperatorsDescription
==Is equal to
!=Is not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
Example:
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)")

Courses
Subscribe Updates via Email

Join 49,000+ W3schools lovers and get all the latest tutorials, programs, algorithms in your inbox.