Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to trim whitespace from a string in Cocoa Touch?

Tags:

I'm looking to determine whether a string value from a user input (UITextField) is "blank" if it's not nil. Checking if [textField.text isEqualToString:""] isn't quite enough because I want to avoid any blank/whitespace input (like say a few space characters).

There does seem to be an indication of a good solution for my particular problem in this StOv post.

Basically it goes something like this, but I suspect there has to (or ought to) be a better way:

NSString *strResult; NSScanner* scanner = [NSScanner scannerWithString:textField.text]; BOOL hasValidChars = [scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]                                               intoString:&strResult];  // if hasValidChars == YES, we've got nonwhite space chars collected into strResult 

This clearly only works for my particular circumstance, and even then it would fail if the first character was a space but the data I wanted followed. So, I realize I've been a little spoiled by Ruby, but there must be a tried and true idiom for trimming strings in Cocoa.

Aaaand the answer is already out there, my apologies:

NSString's -stringByTrimmingCharactersInSet: would do it:

Returns a new string made by removing from both ends of the receiver characters contained in a given character set.

I'm still curious to hear if anybody has other/preferred ways of doing this.

like image 504
Billy Gray Avatar asked Jan 23 '09 19:01

Billy Gray


People also ask

How do I trim Whitespaces from a string?

To trim all trailing whitespace characters (I'm guessing that is actually your intent), the following is a pretty clean & concise way to do it. Objective-C: NSString *trimmedString = [string stringByReplacingOccurrencesOfString:@"\\s+$" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, string.

How do I remove the white space from a string in Objective C?

For those who are trying to remove space in the middle of a string, use [yourString stringByReplacingOccurrencesOfString:@" " withString:@""] .

What is trimming whitespace?

The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

How do I strip whitespace in Swift?

To remove leading and trailing spaces, we use the trimmingCharacters(in:) method that removes all characters in provided character set. In our case, it removes all trailing and leading whitespaces, and new lines.


1 Answers

You're using whitespaceAndNewlineCharacterSet, good. But instead of using scanUpToCharactersFromSet, why not use stringByTrimmingCharactersInSet? Something like this...

strResult = [strResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 

EDIT: Didn't realize you already found stringByTrimmingCharactersInSet until after I posted this.

like image 92
Matt Flowers Avatar answered Sep 30 '22 23:09

Matt Flowers