Skip to main content

Working with strings in swift

String Manipulation in Swift Language


Hello there !!! Welcome to my post about string manipulation in swift.The current post covers the latest swift 4 syntax as specified in the apple developer site. This post will show you all the necessary operations that we need to do at sometime while programming in swift. I hope you will find this post informative and useful. Happy coding :)

String Declaration:

let constantString = "This is a single line string"
//now a multiline string
let quotation = """
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""

String Concatenation:

var firstString = "Hello Everyone."
var secondString = "This is Nikesh's Blog"
var newString = firstString + secondString
print(newString)
//OUTPUT: Hello Everyone.This is Nikesh's Blog

Working with characters and substrings:

var someString = "Hello there"
//Create a string using array of characters
let charArray: [Character] = ["N","I","K","E", "S", "H"]
let srtingFromArray = String(charArray) // "NIKESH"
//Get array of characters from a string
var charatctesFromString = Array(someString) // ["H", "e", "l", "l", "o", " ", "t", "h", "e", "r", "e"]
print(charatctesFromString)
//characters count in string
var numberOfCharacters = someString.count
//print all the characters in a string
for char in someString{
print(char)
}
//H
//e
//l
//l
//o
//
//t
//h
//e
//r
//e
//Access characters at different index
var firstCharacter = someString.first
var lastCharacter = someString.last
//get character at nth index
func getCharAt(index: Int, string: String) -> Character {
let index = string.index (string.startIndex, offsetBy: index)
return string[index]
}
var char1 = getCharAt(index: 6, string: "Apple Inc") // I
//get substring bty specifying and start and end indices
func substring(string: String, fromIndex: Int, toIndex: Int) -> String? {
if fromIndex < toIndex && toIndex < string.count /*use string.characters.count for swift3*/{
let startIndex = string.index(string.startIndex, offsetBy: fromIndex)
let endIndex = string.index(string.startIndex, offsetBy: toIndex)
return String(string[startIndex..<endIndex])
}else{
return nil
}
}
substring(string: "Hello World", fromIndex: 0, toIndex: 3)! // Hel
substring(string: "Hello World", fromIndex: 0, toIndex: 20) // nil

Strings Comparison:

let quotation = "Hello World"
var sameQuotation = "Hello World"
if quotation == sameQuotation {
print("These two strings are considered equal")
}
// Prints "These two strings are considered equal"

Case Conversion in strings

var str = "hello Nikesh"
//Capitalize first letter of every word
var capitalizedStr = str.capitalized // "Hello Nikesh"
//Change string to uppercase
str.uppercased() // "HELLO NIKESH"
//change string to lowercase
str.lowercased() // "hello nikesh"

Comments

Popular posts from this blog

Working with custom views in iOS using swift

The iOS sdk already has a wide collection of UIView subclasses to meet the needs of developer for developing eye-catching and elegant user interface. However, the default UI components provided in the iOS SDK are jus not enough sometimes. In that case, you can create your own UI components as required by writing your code on top of the sdk's classes i.e. UIView.