@paul Here's the example I used:
NSString *yourString = [whitespaceString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
NSString *yourString = [whitespaceString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
I originally did this talk for a Perl conference. As you can imagine, the equivalent code is a LOT shorter in Perl. 😀
@paul Are `whitespaceCharacterSet` and `whitespaceAndNewlineCharacterSet` two different constants in the API? I'm sure I just copied that from the API docs but I don't remember. In any event, in Perl you'd either use `chomp` or a very simple regex if you wanted finer control. That second command would be something like
$yourString =~ s/\s+$//;
in Perl.
@pdcawley @paul I thought the second example was supposed to trim the newline, so that's why I went with that example. My intention wasn't to give every possibility but just demonstrate what the equivalent Perl code might look like. It's easy enough to use a character class in your regex to remove anything you want.
That said, I'm having a hard time thinking of a case where I wanted to trim trailing whitespace and *not* also trim the newlines. Maybe it comes up more often in GUI programming than CLI tools?