特定字符后删除十个空格

时间:2016-09-22 10:13:10

标签: ios objective-c iphone

我有一个来自服务器的字符串:

<p><a href=\"tel:(555) 555-5555\">(555) 555-5555</a>&nbsp;</p> 

我想删除“tel:”之后的任何空格,最多10个字符。

我在下面的代码中尝试过,但没有实现我的目标。

 NSString *serviceMessage = dict[@"message"];

 serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:@"<br />" withString:@"\n"];
 serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:@"<div>" withString:@"\n"];
 serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
 serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:@"</div>" withString:@""];
 serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
 serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
 serviceMessage = [serviceMessage stringByReplacingOccurrencesOfString:@"\n" withString:@" "];

 NSString *mainString = serviceMessage;
 if ([mainString containsString:@"tel:"]) {
      NSUInteger location = [mainString rangeOfString:@"tel:"].location + 4;
      NSString *newString = [mainString substringFromIndex:location];
                        [newString substringWithRange:NSMakeRange(10,0)];
      NSString *newStr =[newString substringToIndex:12];
      NSLog(@"New Trimed string:%@",newStr);
                        newStr = [newStr stringByReplacingOccurrencesOfString:@" " withString:@""];
      NSLog(@"Final Trimed string:%@",newStr);
 }

 serviceMessage = [serviceMessage stringByTrimmingCharactersInSet:
                                      [NSCharacterSet whitespaceAndNewlineCharacterSet]];
 NSString *from = @"From : ";

 NSString *dealerName = dict[@"messageFrom"];

 NSString * append = [NSString stringWithFormat:@"%@%@%@%@",from, dealerName,@"\n",serviceMessage];

2 个答案:

答案 0 :(得分:0)

试试这个

NSString *str1=@"Your Example No = 1";
NSArray *tempArray = [str1 componentsSeparatedByString:@"="];
str1 = [tempArray objectAtIndex:0];
NSLog(@"%@", str1);

输出为Your Example No

希望这有帮助

答案 1 :(得分:0)

如果您想要“tel:”之后的电话号码,可以按照以下方式进行操作

我的代码段:

NSString *str = @"<p><a href=\"tel:(555) 555-5555\">(555) 555-5555</a>&nbsp;</p> ";

NSLog(@"str = %@", str);

//It removes the unwanted extra character like (, ), -
NSCharacterSet *dontInclude = [NSCharacterSet characterSetWithCharactersInString:@"()-"];
NSString *strRemoveSpecial = [[str componentsSeparatedByCharactersInSet: dontInclude] componentsJoinedByString: @""];

//Now strRemoveSpecial is without those special character and we remove the space from the resulted string here
NSString *str1 = [strRemoveSpecial stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"str1 = %@", str1);

//Here we separate string by "tel:" and takes the last part, which contains the telephone number
NSString *strAfterTel = [[strRemoveSpecial componentsSeparatedByString:@"tel:"] lastObject];

//Now substring to 10, which gives us the required telephone number.
NSString *firstTemAfterTel = [strAfterTel substringToIndex:10];

NSLog(@"firstTemAfterTel : %@", firstTemAfterTel);

希望它有所帮助。

快乐的编码......