StringCoding Challenges (Basics) — iOS String Literals

Dipika Kansara
3 min readMar 21, 2022

Basic operations with strings

The String literal in Swift is text within a pair of double quotes.

  1. Declare and Print string Data

In the code shows basic operations of printing a string variable. We can declare a string variable by using string by using annotation.

var myString : String = "My String Data is here"print(myString)

2. Get the number of characters from string

In this code we can check numbers of characters into the string.

var myString : String = "My String Data is here"print(myString.count)

3. Get the type of variable

In this code we can check which type of in string has declared.

var myString : String = "My String Data is here"print(type(of: myString))

4. Append data into string

In this code we can add some data into the string. We can append some data into the string literal to a variable only we could not append the constant. If we try to append a value to a String constant you will get the error

var myString : String = "My String Data is here"myString.append(".")

Also, we can also append data into string by using Arithmetic operation (+).

var myString = "My String Data is here" + "."

In Swift, strings can be changed if you declare them with var. However, if you declare a string to be a constant (keyword let), then it cannot be changed.

Special characters with Strings

The following special characters can be used to append to a String variable.

  1. Newline character :
let details = "I am Dipika \"I am an iOS Developer\"." 
print(details)

2. String Interpolation :

String interpolation is the process of inserting string literals or other data into an existing string literal. The syntax for string interpolation is a backslash followed by a set of parentheses — \().

let pens = 12
let out = "I have \(pens) pens."
print(out)

3.Split a string into an array:

let line = "We will explode the Swift string"
let response1 = line.split(separator: " ")
let response2 = line.split(separator: "e")
print(response1, response2)

4. Omitting Empty Subsequences :

Generally we ignore the white spaces, but there is an optional parameter omittingEmptySubsequences. If we set that to false the returned array will have empty space elements.

let line = "We will          explode the Swift string" print(line.split(separator: " ")) 
print(line.split(separator: " ", omittingEmptySubsequences: false))

5.String splitting by word delimiter:

let line = "We will explode the Swift string"  
let splits = line.components(separatedBy: "in") print(splits)

Join the string from array of strings (characters)

let array = ["We", "Will", "explore", "the", "Swift", "string", "!"] let joined = array.joined(separator: " ") 
print(joined)

Reverse the string

let str = "Exploring Swift"
var rev = String(str.reversed())
print(reversed)

Check if string contains the substring

One very useful thing operation in Swift would be to check if a substring is part of a string. For that Swift has the contains method.

var myString = "Let's learn Swift Language!" print(myString.contains("language")) 
print(myString.contains("Swift")

Today we learned Coding challenges for string literals in Swift.

Thanks for reading!

Happy coding😁

--

--