Sometimes manipulating strings can be a little cumbersome in Swift. Here’s a handful of techniques you might find useful.

Split

Split a string into an array.

let text = "abc123"
let letters = Array(text)
// letters = ["a", "b", "c", "1", "2", "3"]

Sort string

First, we’ll split the string, then sort it, then join back together. In this example, I’m mapping the Array<Character> to Array<String> so it can be sorted easier. It’s possible this isn’t necessary, but I’ve yet to find a better way.

let text = "zxy321"
let letters = Array(text).map({ String($0) })
let sortedText = join("", letters.sorted())
// sortedText = "123xyz"

Trim whitespace

Remove all leading and trailing whitespace from a string.

let spaceSet = NSCharacterSet.whitespaceCharacterSet()
let trimmed = "  goo  ".stringByTrimmingCharactersInSet(spaceSet)
// trimmed == "goo"

There are lots of other character sets including:

  • alphanumericCharacterSet
  • whitespaceCharacterSet
  • symbolCharacterSet
  • punctuationCharacterSet

In addition, you can get the opposite of any character set by adding .inverted to the end.

Remove substrings

Remove all occurrences of a substring by using a regular expression.

let dirty = "Pass the $!#@ salsa please"
let clean = input.stringByReplacingOccurrencesOfString(
  "[\\$\\!\\#\\@]",
  withString: "",
  options: .RegularExpressionSearch)
// clean == "Pass the salsa  please"

Remove duplicate whitespace

We can also use RegularExpressionSearch to remove duplicate whitespace characters.

let fitted = "Too    much".stringByReplacingOccurrencesOfString(
  "\\s+",
  withString: " ",
  options: .RegularExpressionSearch)
// fitted == "Too much"

Substring

Get a substring starting from the beginning of the string until index X.

let haystack = "You only care about yourself"
let endIndex = advance(haystack.startIndex, 3)
let needle = haystack.substringToIndex(endIndex)
// needle == "You"

Get a substring from index X to the end of the string.

let haystack = "Jump to the end"
let startIndex = advance(haystack.startIndex, 12)
let needle = haystack.substringFromIndex(startIndex)
// needle == "end"

Prefix/suffix

Check whether a string starts with a given substring.

if "Asking or telling?".hasSuffix("?") {
  println("He's asking")
}

Similarly, we can check whether a string ends with a substring.

if "Dude, I know".hasPrefix("Dude") {
  println("Dude...")
}

Check for substring

Find the range of a string inside another string. This can also act as a Regex test method when passed the .RegularExpressionSearch option.

let range = "Buy 2 magic beans".rangeOfString(
  "[0-9]",
  options: .RegularExpressionSearch)
// range == Range(4, 5)

Further reading

Here are some articles on Swift techniques I found helpful: