Show Menu

Looking for an iOS Developer?

Submit your 30 day Job Listing for FREE

Swift Tutorial

This part of the iOS 8 Swift Programming Language tutorial Series covers Strings. Like most programming languages Strings are extremely useful. Find out how to use strings in Swift here.

Here is a list of the other articles in this series already published:

Please stay tuned for updates and new additions by following us on twitter , Liking us on facebook or signing up to our Monthly Mailing List

Strings in Swift

So, firstly what is string? Well, a String is an ordered collection of characters. Like NSString in Objective-C we have the String type in Swift and for characters we have Character type. String is a combination of characters.

You can create string in Swift like so:


var  myName = "iPhone Development"

If you would like to initialize an empty string in swift, then you would do this:


var  myName = " "

var initializemyName = String() 

Here myName and initializemyName are both empty strings and the only difference between them is that myName is literally string and the second line is a syntax to initialize a string in swift.

Strings can be mutated (concatenated) in swift as we are doing in Objective-C.


var myName = "iPhone "

myName += "Development"

Now the output of myName would be:


// This ouputs the string
println("output of myName = \(myname)")

//Result: output of myName = iPhone Development

Strings declared as variable or var can be mutated and strings declared as constant or let are immutable strings. The below code will throw error at compile time that a constant string cannot be modified


let myName = "iPhone"

myName += "Development"

You can check for empty string by using isEmpty property of a string, this will return a Boolean value to you


var emptyString = ""

if emptyString.isEmpty
{
    println("String is empty")
}

Initialise a string from NSData in Swift

So, lets say that you wish to convert the following from Obcjective-C to Swift:


NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];

You would Initialise it like so:


var datastring = NSString(data data: NSData!, encoding encoding: UInt)

Simple, isnt it :)

See Setting Initial Values for Stored Properties for further information and advice.

Compare Strings in Swift

Swift provides three ways to compare strings:

  • string equality
  • prefix equality
  • suffix equality

Using the if statement, we can check the string qualirty in Swift like so:


// Declare both strings
var firstString = "We are same"
var secondString = "We are same" 

Then use the if statement mentioned earlier to check that they are the same:


if firstString = secondString
{
    println("Both strings are same")
    // This will only display if they ARE the same
}

To check prefix in Swift we can use:


if firstString.hasPrefix("We")
{
    println("First string contains we")
} 

To check suffix in Swift we can use:


if secondString.hasSuffix("same")
{
    println("Second string contains same")
} 

UPPERCASE and lowercase in Swift

We can convert string to both uppercase and lowercase in Swift by using the properties uppercaseString and lowercaseString


let string = " normal"

let resultedString = string.uppercaseString
println("result uppercase=\(resultedString) )

resultedString = string.lowercaseString
println("result lowercase=\(resultedString) ) 

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


Written by:

I am a iPhone developer having 3+ years of working experience in developing iPhone/iPad apps.

Comments

comments