View Full Version : Assistance with removing characters from string

Apr 15, 2009, 11:02 PM
Hi, I am new at this, and have been struggling through app development. I have a problem with some of my code. The user can choose a person's phone number in my app, and the phone number is inserted into a field. I need the phone number to be inserted into the field with no formatting. Basically like: 2123453423 instead of how is is currently being inserted like: (212) 345-3423. If anyone has suggestions as to how I can get my code to work, I'd be very appreciative. Here is what I have so far:

- (IBAction) adressBook: (id) sender; {
ABPeoplePickerNavigationController *pp = [[ABPeoplePickerNavigationController alloc] init];
pp.displayedProperties = [NSArray arrayWithObject:[NSNumber numberWithInt:kABPersonPhoneProperty]];
pp.peoplePickerDelegate = self;
[self presentModalViewController:pp animated:YES];
[pp release];
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
// assigning control back to the main controller
[self dismissModalViewControllerAnimated:YES];

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
return YES;

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier {

ABMultiValueRef phoneProperty = ABRecordCopyValue(person,property);
NSString *phone = (NSString *)ABMultiValueCopyValueAtIndex(phoneProperty,identifier);

[[[[phone stringByReplacingOccurrencesOfString:@"(" withString:@""]
stringByReplacingOccurrencesOfString:@")" withString:@""]
stringByReplacingOccurrencesOfString:@"-" withString:@""]
stringByReplacingOccurrencesOfString:@" " withString:@""];

phonenumber.text = phone;

[phone release];

[self dismissModalViewControllerAnimated:YES];
return NO;