How to trim a string in Swift with examples

The string contains spaces at the start or end of a string. This post explains about following things.

  • Trim whitespace in a string
  • Delete leading space in a string
  • remove trailing space in a string
  • Remove a string that contains spaces in the middle or any.

String trim whitespace example

This example removes leading and trailing spaces from a given string. The trimmingCharacters method with whitespaces works in Swift version > 4.

import Foundation
let str=" one two three ";
print(str)

let result = str.trimmingCharacters(in: .whitespaces)
print(result)

Output:

 one two three
one two three

In the Swift 3 version, you can use CharacterSet.whitespacesAndNewlines in trimmingCharacters method as given below

import Foundation
let str=" one two three ";
print(str)

let result = str.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
print(result)

You can see, It does not remove spaces in a string.

You can add a trim method to the String class using the extension given below.

import Foundation
extension String{
func trim() -> String{
return self.trimmingCharacters(in: .whitespaces)
}
}
let str=" one two three ";
print(str)
print(str.trim())

How to remove all the leading(Start) spaces from a string in Swift

This example removes leading(Start) from a given string.

  • Created an extension to the String class
  • Added function trimStart() function, removes leading space and return string. It removes all spaces at the start of a string
  • Checks string contains space at the start using while loop with hasPrefix method.
  • Return the substring by excluding the last element using the dropFirst function
  • Finally, Returns the string Here is an example of leading trim in a string with the example
import Foundation
extension String{

  func trimStart() -> String {
        var str = self
        while str.hasPrefix(" ") {
          str = "" + str.dropFirst()
        }
        return str
    }
}
let str=" one two three ";
print(str)
print(str.trimStart())

Output:

one two three
one two three

How to remove trailing(End) spaces from a string in Swift

This example removes trailing(end) from a given string.

  • Created an extension to the String class
  • Added function trimEnd() function, removes trailing end space and return string. It removes all spaces at the end of a string
  • Checks string contains space at the end using while loop with hasSuffix method.
  • Return the substring by excluding the last element using dropLast function
  • Finally, Returns the string

Here is an example of trailing trim in a string with the example

import Foundation
extension String{

  func trimEnd() -> String {
        var str = self
        while str.hasSuffix(" ") {
          str = "" + str.dropLast()
        }
        return str
    }

let str=" one two three ";
print(str)
print(str.trimEnd())

Output:

 one two three
 one two three

how to remove spaces from a string

This example removes space characters from a string. The string contains the leading, trailing, or middle of a string.

Here is an example

import Foundation
extension String{
  func removeSpace()-> String{
    return  self.replacingOccurrences(of: "\\s", with: "", options: .regularExpression)

  }
}
let str=" one two three ";
print(str)
print(str.removeSpace())

Output:

one two three
onetwothree