Swift Strings

  Total Views : 366
  Comments : 0
  Keywords : Swift strings are the collection of characters with examples, Swift define strings with data type String with examples, Swift concatenate string characters with example

Here we will learn strings in swift with examples and how to use swift strings, declaring strings in swift, string concatenation in swift, strings interpolation in swift, swift string comparisons and strings mutability in swift with examples.

Swift Strings

In swift string is a collection of characters like “Welcome to Tutlane” or “Hello World” and strings are represented by data-type called “String”.

 

Generally, in swift we use data-type “String” to hold and store the given string value in computer memory to perform data manipulations on given string based on our requirements.

Swift Strings Declaration

In swift we can declare string using string literal (“”) or string instance (String(“”)) based on our requirements.

 

Following is the syntax of defining string in swift programming language using string literal and String instance.

 

var <name>: <Datatype> = "Your String"

 

or

 

var <name> = String("Your String")

If you observe above syntax we used two ways to define string one is string literal ("") and another one is string instance String("Your String").

 

Now we will see the simple example of declaring string in swift programming using both string literal and string instance.

 

Following is simple example of declaring string using string literal and string instance in swift programming language.

 

// using string literal

let str1: String = "Welcome to Tutlane"

print(str1)

// using String instance

let str2 = String("Welcome to Tutlane")

print(str2)

When we run above example in swift playground we will get result like as shown below.

 

Welcome to Tutlane

Welcome to Tutlane

Swift Empty Strings

Generally in any programming language we will assign empty string value while declaring string variables. In swift we can assign empty string value in two ways one is using string literal ("") another one using string instance (String()).

 

In swift we can easily check if string variables contains empty string or not using isEmpty property.

 

Following is the simple example of assigning empty string value to the string variables using string literal ("") and string instance (String()) in swift programming language.

 

// using string literal

let str1: String = ""

if str1.isEmpty {

print("str1 is empty")

}

else {

print("str1 is contains text")

}

 

// using String instance

let str2 = String()

if str2.isEmpty {

print("str2 is empty")

}

else {

print("str2 is contains text")

}

When we run above example in swift playground we will get result like as shown below.

 

str1 is empty

str2 is empty

Swift Strings Concatenation

In swift by using addition operator (“+”) we can concatenate strings based on our requirements.

 

Following is the simple example of concatenating strings in swift programming language.

 

var str1 = "Welcome to "

var str2 = "Tutlane"

var result = str1 + str2

print(result)

The above swift program will return the result like as shown below.

 

Welcome to Tutlane

In swift we can append string value to an existing string variable using addition assignment operator (+=). 

 

Following is the example of appending string value to an existing string variable using addition assignment operator (+=).

 

var str1 = "Hello" + " My name is "

let str2 = "Suresh Dasari"

str1 += str2

print(str1)

When we execute above program we will get result like as shown below.

 

Hello My name is Suresh Dasari

We can append characters to the string using append() property. Following is the simple example of appending character to the string.

 

let char: Character = "!"

str1 += String(char)

print(str1)

When we execute above program we will get result like as shown below.

 

Hello My name is Suresh Dasari!

Swift Strings Interpolation

In swift string interpolation means creating new string value from a mix of numbers, constants, variables, literals and expressions by including their values inside a string literal.

 

Every time when we insert into string literal, it is wrapped into pair of parentheses and prefixed by backslash (\).

 

Following is the simple example of implementing string interpolation in swift programming language.

 

let name = "Suresh Dasari"

let strinter = "Hello My Name is \(name)!"

print(strinter)

When we execute above swift program in playground we will get result like as shown below

 

Hello My Name is Suresh Dasari

Following is another example of string interpolation in swift programming.

 

let division = 1.0 / 4.0

let strnumber = "The Division is \(division) as a decimal."

print(strnumber)

When we execute above swift program we will get result like as shown below.

 

The Division is 0.25 as a decimal.

Swift Strings Mutability

In swift we can define whether the given string can be modified or mutated while assigning it to a variable or it can be constant (in which case it cannot be modified) using let keyword.

 

Following is the simple example of implementing string mutability in swift programming language.

 

// variable can be modified

var str1: String = "Welcome"

str1 += " to Tutlane"

print(str1)

When we run above program we will get result like as shown below.

 

Welcome to Tutlane

Following is another example of string interpolation in swift programming language.

 

// constant variable not be modified

let str2: String = "Welcome"

str2 += " to tutlane"

print(str2)

If you observe above example we are trying to append text to constant variable. When we try to run above program the swift playground will throw error like as shown below.

 

compile-time error - a constant string cannot be modified

Swift Strings Comparison

In swift by using equal to (==) and not equal (!=) operators we can check whether the given two strings are equal or not. 

 

The equality operators will perform case-sensitive comparison that means given strings must be in either lower or upper case otherwise it will return as strings are not equal.

 

Following is the example of string comparison in swift programming language.

 

let str1 = "khawar"

let str2 = "suresh"

if str1 == str2 {

print("Both are Equal")

}

else {

print("Given strings are not equal")

}

When we run above example in swift playground we will get result like as shown below.

 

Given strings are not equal

Following is another example of comparing given strings in swift programming language.

 

let str1 = "Tutlane"

let str2 = "tutlane"

if str1 == str2 {

print("Both are Equal")

}

else {

print("Given strings are not equal")

}

When we run above example in swift playground we will get result like as shown below.

 

Given strings are not equal

This is how we can use strings in swift to compare strings, append strings, check for string value, etc. based on our requirements. 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User