Reputation: 5232
I am trying to get Numeric from the string. I have string with phone number so I need numerics only.
My string format is:
P: 000-000-0000
So I am using following code for that:
[[strPhone componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:@""];
In few cases I am getting following format for my string:
P: 000 000 0000 M: 000 000 0000
What I want to do is.. I want to split my string in an array. for that purpose I need to split my string from the characters.
Is there any way to achive this thing.
In case that if we have
P: 000 000 0000 Mo: 000 000 0000 format then the array should have only two parts. One should split with "P" and second with "Mo". not the three part one with "P", Second "M", third "o".
Plz help me to achive this.
Thanks
Upvotes: 1
Views: 598
Reputation: 8482
You could split up the data first to detect multiple numbers in one string using
NSArray *candidates = [strPhone componentsSeparatedByString:@":"];
Then loop over the candidates and perform your previous phone number check and remove all empty strings from the result, e.g.
for (NSString *candidate in candidates) {
NSString *result = <what you did before>;
if ([result length] > 0) {
<add to results array>;
}
}
Upvotes: 0
Reputation: 557
Use following code :
NSString *list = @"Norman, Stanley, Fletcher"; NSArray *listItems = [list componentsSeparatedByString:@", "];
The listItmes contains all the separated strings.
Upvotes: 0
Reputation: 5050
I assume you have a string with some text and a phone number. In this case you can use the NSDataDetector class. Like this:
NSError *error = NULL;
NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypePhoneNumber
error:&error];
Then get the results array (phone numbers) by:
NSArray * phoneNumbers = [detector metchesInString:YOURSTRING options:0 range:NSMakeRange(0, [YOURSTRING length])];
Upvotes: 1