G Alexander G Alexander - 9 months ago 36
Swift Question

Cleanest way to remove trailing blanks a string in swift?

In Perl if I had trailing blanks in a string, I would use this code

$line =~ s/ +$//;

Is there a similar command or function in swift?


Swift provides no support for regular expressions. It doesn't need to, though, because the only place you'll ever use Swift is in the presence of Cocoa — and Cocoa's Foundation framework does provide support for regular expressions (and has other string-handling commands). Swift String and Framework's NSString are bridged to one another.

Thus, you could use stringByReplacingOccurrencesOfString:withString:options:range: with a regular expression pattern, for example (assuming that stringByTrimmingCharactersInSet: is too simple-minded for your purposes).