String in Swift3

September 27, 2017

How to define string variable

var str: String = "Hello World"

Escape Characters

\\ => If you want to show back slash

var str: String = "Hello \\World"
print (str)

Output
Hello \World

\t => Shows the tab

var str: String = "Hello \tWorld"
print (str)

Output
Hello World

\n => show new line
\” => shows double quote
\’”=> shows single quote

Joining two strings

var str1: String = "string 1 "
var str2: String = "string 2 "
var str3 = str1 + str2
print (str3)

Get the length of String

var str: String = "string 1 "
print ("Length of the string : \(str.characters.count)")

Output
Length of the string : 9