Chapter 03: String in Swift

All About String in Swift Programming Language

Let’s know what String is. String is a collection of characters

Here is how to declare a String

var courseName = "iOS Development"
var language = "Swift"

Here is how to use functions like String Concatenation, i.e. attaching one String to a second string

var hello = "Hello, "
let world = "World! "
let sentence = hello + world + "This is iOS Development" // "Hello, world! This is iOS Development"

Important Note:
As you can see above, we have used the ‘+’ operator to concatenate two strings together

Here is how to use functions like String Interpolation, i.e., a way to write some code in the middle of the String. The returned value of the code will become part of the String.

var courseName = "iOS Development"
var language = "Swift"
var software = "Xcode"
var emotionEmoji = "😍" // Unicode Character i.e. Emoji Icon
var swiftVersion = 3.0 // Double or Floating point Number
var fact = "\(courseName) in \(language) with \(software) is so fun! \(emotionEmoji) & we are using swift with version \(swiftVersion)" // So it will read as "iOS Development in Swift with Xcode is so fun! 😍 & we are using swift with version 3.0"

String Properties & Call methods using dot notation/operators

Here is how to check if the String is empty

fact.isEmpty // false

Convert all characters of String into upper / lower case

var name = "TheIronMan" name.lowercased() // It will appear as "theironman"
name.uppercased() // It will appear as "THEIRONMAN"

Here is how we can compare if two strings are identical. For this, we will use the logical equivalent (==) operator

let mySchool = "Harvard"
let yourSchool = "Stanford"
mySchool == yourSchool // false

I hope this blog post will help you to understand Strings in Swift Programming Language better,

Thanks & Regards
Mandar Apte

Published by Mandar Apte

Mandar is a Mumbai-based multi-disciplinary designer with UX/UI, Logo, Symbol, and Brand Identity design expertise. He currently runs his Mudrkashar Linguistic Apple iPhone, iPad, and Mac app business in the heart of Mumbai city.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.