Remove white space from contact number fetched from phone book

*爱你&永不变心* 提交于 2019-12-02 01:35:59

问题


I know its a very basic question, but I am stuck at this point.

I am fetching contact numbers from phonebook and having them in an array.

And then fetching a particular one from array in NSString

And I am trying to remove white space from the number, but it is not actually with the below written codes:

NSString *num = @"+44 123 456 7890";

num = [num stringByReplacingOccurrencesOfString:@" " withString:@"0"];

**OR**

num = [num stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

Please let me know what I need to do to remove this space.

Thanks

EDIT

NSString *num = [contactNumbers objectAtIndex:i];
        num = [num stringByReplacingOccurrencesOfString:@"-" withString:@""];
        num = [num stringByReplacingOccurrencesOfString:@" " withString:@""];
        num = [num substringFromIndex:[num length]-10];

回答1:


Done the trick :

NSString * strippedNumber = [num stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [number length])];



回答2:


Try this:

// Replace @"0" with @""
num = [num stringByReplacingOccurrencesOfString:@" " withString:@""];

Using stringByTrimmingCharactersInSet only trims white spaces from the beginning and end of string.

EDIT

NSString *num = [contactNumbers objectAtIndex:i];
NSString *newString = [num stringByReplacingOccurrencesOfString:@"-" withString:@""];
newString = [newString stringByReplacingOccurrencesOfString:@" " withString:@""];
newString = [newString substringFromIndex:[num length]-10];


来源:https://stackoverflow.com/questions/25584070/remove-white-space-from-contact-number-fetched-from-phone-book

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!