开发者

iphone+string operation

i am having an string like bellow:

J B Tower, Drive In, Ahmedabad, &# 2327; &# 2369; &# 2332; &# 2352 ;&# 2366 ;&# 2340 ;

380054 (Kabir Restaurant),

Now i want to remove this --

&# 2327 ;&# 2369 ;&# 2332 ;&# 2352 ;&# 2366 ;&# 2340 ;

i had used this operation:-

NSString *substring = nil开发者_如何学JAVA;  
NSRange newlineRange = [lblAddress.text rangeOfString:@"&#"];   
if(newlineRange.location != NSNotFound) 
{   
substring = [TargetString substringFromIndex:newlineRange.location];        
//[substring stringByReplacingOccurrencesOfString:substring withString:@""];        
TargetString=[TargetString stringByReplacingOccurrencesOfString:substring withString:@""];
}

As a result i got the TargetString===>J B Tower, Drive In, Ahmedabad, substring===>&# 2327 ;&# 2369 ;&# 2332 ;&# 2352 ;&# 2366 ;&# 2340 ; 380054 (Kabir Restaurant),

But i want the string as====> J B Tower, Drive In, Ahmedabad,380054 (Kabir Restaurant),

Please help me.


NSString *s =[NSString stringWithFormat:@"%@", @"J B Tower, Drive In, Ahmedabad, &# 2327; &# 2369; &# 2332; &# 2352 ;&# 2366 ;&# 2340 ;380054 (Kabir Restaurant),"];

NSArray * split=[s componentsSeparatedByString:@","];

int  tempIndex = (int)([[split objectAtIndex:3] rangeOfString:@";" options:NSBackwardsSearch].location);
NSString * str=[[split objectAtIndex:3] substringToIndex:tempIndex+1];

s=[s stringByReplacingOccurrencesOfString:str withString:@""];

NSLog(@"%@", s); 


Use below function.

-(void)getFinalString:(NSString *)pstrMainString:(NSString *)pstrRemovedString{

NSString *strFinalString;

NSArray *arraySplitted = [pstrMainString componentsSeparatedByString:pstrRemovedString];
NSMutableArray *arrayFinalArray = [NSMutableArray arrayWithArray: arraySplitted];

if([arrayFinalArray count]==1){
    strFinalString = [arrayFinalArray objectAtIndex:0];
}
else if([arrayFinalArray count]==2){
    strFinalString = [arrayFinalArray objectAtIndex:0];
    strFinalString = [strFinalString stringByAppendingFormat:@"%@", [arrayFinalArray objectAtIndex:1]];
}

NSLog(@"%@", strFinalString);
}

Here, pstrMainString = "J B Tower, Drive In, Ahmedabad, गुजरात 380054 (Kabir Restaurant),"

And pstrRemovedString = "गुजरात"

It will display final string which you want.

Let me know in case of any difficulty.

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜