PDA

View Full Version : the best way to "clean" a string?




patrover
Sep 7, 2009, 06:31 AM
i have a string: (2434)-45455

and i want to filter out all the non-numeric chars ( i.e "()-" ), what is the best way?


thanks



dejo
Sep 7, 2009, 12:54 PM
Have you looked through the NSString Class Reference (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html) or the String Programming Guide for Cocoa (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/introStrings.html)?

MacDonaldsd
Sep 7, 2009, 02:13 PM
Have you looked through the NSString Class Reference (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html) or the String Programming Guide for Cocoa (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Strings/introStrings.html)?

Thats obviously the best place to start. If you read through them you will find.

- (NSString *)stringByTrimmingCharactersInSet:(NSCharacterSet *)set;

PhoneyDeveloper
Sep 7, 2009, 03:04 PM
stringByTrimmingCharactersInSet: probably won't be useful for this purpose.

Look at the string programming guide, as suggested. Also look at NSScanner. If you prefer regular expressions you could look into that as well.

dejo
Sep 7, 2009, 07:48 PM
stringByTrimmingCharactersInSet: probably won't be useful for this purpose.
Just curious: why do you say this?

patrover
Sep 8, 2009, 06:43 AM
if i'm not mistaken trimming is only for the edges of the string.

dejo
Sep 8, 2009, 09:27 AM
if i'm not mistaken trimming is only for the edges of the string.
Ah, that's right. Perhaps one of the stringByReplacing... methods will suit your needs.

RaceTripper
Sep 8, 2009, 09:36 AM
Can you use regex? Use that to replace all [^\d] with the empty string.

In perl it would be: s/[^\d]//g