2016-09-22 10 views
0

た後、私は、サーバーから来ている文字列を持つテンスペースを削除します。特定の文字

<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

あなたの問題は何ですか?あなたの努力を表示してください。 – Andy

+0

あなたは既にこのquesを尋ねてきました。あなたはすでにあなたの問題に関して十分な反応を得ています。[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

答えて

0

この

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

出力は、これはあなたが "TEL:" の後に電話番号が必要な場合

0

を助けYour Example No

希望で試してみてください、あなたは次のようにそれを行うことができます

マイコードスニペット:

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

希望します。

ハッピーコーディング...

関連する問題