从电话簿中提取的联系号码中删除空格

我知道这是一个非常基本的问题,但我现在陷入困境 .

我从电话簿中取出联系电话号码并将它们放在一个数组中 .

然后从NSString中的数组中获取特定的一个

我试图从数字中删除空格,但实际上并没有使用以下编写的代码:

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

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

**OR**

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

请让我知道我需要做些什么来删除这个空间 .

谢谢

EDIT

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

回答(2)

2 years ago

完成诀窍:

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

2 years ago

试试这个:

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

使用 stringByTrimmingCharactersInSet 仅修剪字符串开头和结尾的空格 .

EDIT

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