G Alexander G Alexander - 1 year ago 91
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?

Answer Source

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).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download