Meet Strings in Swift as iOS Developer

Sai Durga Mahesh
May 16 · 3 min read
Photo by Mika Baumeister on Unsplash

Strings in swift are very commonly used by iOS Developers. We solve variety of problems in our development with the power of strings. But we generally overlook some of important functions in strings. In this article I try to collect such functions and add some extensions possibly.

Separate substrings from string

let res = str.components(separatedBy: ",")

output is array of strings which are separated by ‘,’ in original string.

Index based use cases

One prominent reason for this is length of characters in swift is not uniform.Combination of unicode points to make up character varies. so indexes are calculated with respect to string every time.

Index … After

extension Array where Element == Character {  func index(of character: Character, after index: Int) -> Int? {    for i in index..<self.count where self[i] == character {       return i    }    return nil  }}

Partial Replacement

str.replacingOccurrences(of: "xcode", with: "Xcode")

Also if we want to replace the string between two given characters or sub strings, we can use following.

guard let startIndex = str.firstIndex(of: "a") else { return false}
guard let endIndex = str.firstIndex(of: "o") else { return false}
str.replaceSubrange(startIndex...endIndex, with: "oil")

Boundary Testing

From starting

str.hasPrefix("i")// testing that a string is lexicographically greater than otherstr.lexicographicallyPrecedes("zzz")

From ending

str.hasSuffix("i")

Difference between two strings

str = "oil"let dif = str.difference(from: "oii")print(dif) /* CollectionDifference<Character>(insertions: [Swift.CollectionDifference<Swift.Character>.Change.insert(offset: 2, element: "l", associatedWith: nil)], removals: [Swift.CollectionDifference<Swift.Character>.Change.remove(offset: 2, element: "i", associatedWith: nil)]) */print(dif.insertions.count+dif.removals.count) // 2

Maximum

str = "aAB"str.max() // a

Be aware that small letters are considered maximum.

Count of Decimals/lower case letters/ upper case letters/….(CharacterSet)

extension String {  func count(of characterSet: CharacterSet) -> Int {    var count = 0    var range = self.rangeOfCharacter(from: characterSet)    while range != nil {      count = count + 1      guard let ub = range?.upperBound else {         break      }      range = self[ub...].rangeOfCharacter(from: characterSet)    }    return count   }} 

That’s it…

Thanks for reading…

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Sai Durga Mahesh

Written by

Love playing with data

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/. Don’t forget to check out Ask-NFT, a mentorship ecosystem we’ve started

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store