我是IOS开发的新手。我在NSArray获得了电话号码,这就是这样的。
9429564999,
9428889239,
7878817072,
"+919408524477",
9909951666,
9879824567,
"+91 8469-727523",
"94-28-037780",
"+918460814614",
55246,
"8866-030880",
"95-37-223347",
"+919574777070",
"+917405750526",
现在我要从NSArray列表中删除 - 和 +91 而不是整个对象。如何做到这一点请有人帮助
答案 0 :(得分:2)
for (NSString *number in arrayName) {
number = [number stringByReplacingOccurrencesOfString:@"+91"
withString:@""] mutableCopy];
number = [number stringByReplacingOccurrencesOfString:@"-"
withString:@""] mutableCopy];
[arrayNameNew addObject:number];
}
试试这样:)
快乐编码。
答案 1 :(得分:1)
for (NSString *phoneNum in phoneNumArray) {
phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"+91" withString:@""];
phoneNum = [phoneNum stringByReplacingOccurrencesOfString:@"-" withString:@""];
}
答案 2 :(得分:0)
试试这个
for (NSString *str in arrayNumbers) {
NSString *stringWithoutNineOne = [str stringByReplacingOccurrencesOfString:@"+91" withString:@""];
NSString *stringWithoutSpace = [stringWithoutNineOne stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *stringWithoutdash = [stringWithoutSpace stringByReplacingOccurrencesOfString:@"-" withString:@""];
[refinedArr addObject:stringWithoutdash];
}
答案 3 :(得分:0)
从数组中删除特殊字符。
for (NSString *strPhoneNo in arrayPhoneNo) {
[self removeSpecialCharacter:strPhoneNo];
}
和
-(NSString*)removeSpecialCharacter:(NSString*)mobileNumber
{
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+91" withString:@""];
mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
// remove extra special charecter if needed.
//mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
//mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
//mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
return mobileNumber;
}
如果NSArray没有更新,那么使用NSArray的NSMutableArray。
答案 4 :(得分:0)
你也可以尝试这个.... 获取给定电话号码的最后10位数.....如果您的要求已修复,请尝试使用此单行代码
for (NSString *string in arrayNumbers) {
NSString *trimmedString=[string substringFromIndex:MAX((int)[string length]-10, 0)];
[newArray addobject:trimmedString]
}
答案 5 :(得分:0)
NSArray *phoneNoList = [[NSArray alloc]initWithObjects:@"9429564999",@"9428889239",@"7878817072",@"+919408524477",@"9909951666",@"9879824567", @"+91 8469-727523",@"94-28-037780",@"+918460814614",@"55246",@"8866-030880",@"95-37-223347",@"+919574777070", @"+917405750526", nil];
NSMutableArray *noArr = [[NSMutableArray alloc]init];
for (NSString *no in phoneNoList) {
NSString *temp1 = [[no componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]]
componentsJoinedByString:@""];
if (temp1.length > 10) {
NSString *temp=[temp1 substringFromIndex:2];
[noArr addObject:temp];
}
else
[noArr addObject:temp1];
}
NSLog(@"Arr Data : %@",noArr);
试试这个,绝对有助于你。