0

I have an string which is coming from a server :

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

I want to remove any space after "tel:" up to 10 characters.

I tried this below code but didn't achieve my goal.

 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];
Sailendra
  • 1,318
  • 14
  • 29
AADi
  • 19
  • 4
  • 2
    What was your problem? Display your efforts. – pronebird Sep 22 '16 at 10:16
  • you have already asked this ques before and you have already got enough response regarding you problem ..[http://stackoverflow.com/questions/39632321/remove-space-after-a-particular-character/39632578#39632578](http://stackoverflow.com/questions/39632321/remove-space-after-a-particular-character/39632578#39632578) – vaibhav Sep 22 '16 at 11:32

2 Answers2

0

Try this

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

Output is Your Example No

Hope this helps

Kamlesh Shingarakhiya
  • 2,757
  • 2
  • 16
  • 34
0

If you want the telephone number after the "tel:", you can do it in following way

My code Snippet :

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);

Hope it helps.

Happy coding ...

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43