Trim whitespace from a string

 Problem

I want to remove all leading and trailing whitespace from a string.



 Recipe


// String literal
var s = "    Hello World!     "

// Trim all whitespace

s = s.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

print(s)
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground