iphone - NSRangeException even the range is correct in NSString Replace -


i have written code replace delete substring in nsstring...my logic use 'rangeofstring' find position of each substring characters , using 'replaceoccurance' replace substring characters space , later trimm whitespace character. getting error in replaceoccurance line

*** terminating app due uncaught exception 'nsrangeexception', reason: '-[__nscfstring replaceoccurrencesofstring:withstring:options:range:]: range or index out of bounds' *** first throw call stack: (     0   corefoundation                      0x00007fff85614b06 __exceptionpreprocess + 198     1   libobjc.a.dylib                     0x00007fff83c4e3f0 objc_exception_throw + 43     2   corefoundation                      0x00007fff856148dc +[nsexception raise:format:] + 204     3   corefoundation                      0x00007fff85613520 -[__nscfstring replaceoccurrencesofstring:withstring:options:range:] + 224     4   foundation                          0x00007fff8d433a12 -[nsstring stringbyreplacingoccurrencesofstring:withstring:options:range:] + 170     5   tester                              0x00000001000019a1 deletewithsubstring + 865     6   tester                              0x0000000100001d35 main + 133     7   libdyld.dylib                       0x00007fff89e267e1 start + 0     8   ???                                 0x0000000000000001 0x0 + 1 ) libc++abi.dylib: terminate called throwing exception.... 

here code.

nsstring* deletewithsubstring(nsstring *s1,nsstring *sub) {     long int length=[sub length],range=0;     nsrange r;     for(int i=0;i<length;i++)     {         range=[s1 rangeofstring:[nsstring stringwithformat:@"%c",[sub characteratindex:i]]options:nscaseinsensitivesearch range:nsmakerange(range,[s1 length]-1)].location;           r=nsmakerange(range,[s1 length]-1);         nslog(@"%ld %ld",r.location,r.length);        if(range!=nsnotfound)        {            s1=[s1 stringbyreplacingoccurrencesofstring:[nsstring stringwithformat:@"%c",[sub characteratindex:i]] withstring:@" " options:nscaseinsensitivesearch range:r];        }     }     s1=[s1 stringbytrimmingcharactersinset:[nscharacterset whitespacecharacterset]];     return s1; }  int main(int argc, const char * argv[]) {      @autoreleasepool {           nsstring *str1,*str2;         str1=@"hdida";         str2=@"fhsdai";          nslog(@"loc b:%@ ",,deletewithsubstring(str2,@"ha"));       }     return 0; } 

actually solved problem range..as rewriting code mistook seocond argument not upperbound rather length ..which should follows

   nsstring* deletewithsubstring(nsstring *s1,nsstring *sub)     {         long int length=[sub length],range=0,ndeleted=0;         nsmutablestring *s2=[[nsmutablestring alloc]initwithstring:s1];         nsrange r;         for(int i=0;i<length;i++)         {             range=[s1 rangeofstring:[nsstring stringwithformat:@"%c",[sub characteratindex:i]]options:nscaseinsensitivesearch range:nsmakerange(range,[s1 length]-range)].location;               r=nsmakerange((range-ndeleted),1);             nslog(@"%ld %ld" ,r.location,r.length);            if(range!=nsnotfound)            {                [s2 deletecharactersinrange:r];                ndeleted++;            }         }          nslog(@"%@",s2);         return s2;     } 

try following code,

nsstring* deletewithsubstring(nsmutablestring *s1,nsstring *sub) {      nsrange r;     for(int = 0; < [s1 length]; i++)     {         //findout range "sub" string         r = [s1 rangeofstring:sub options:nscaseinsensitivesearch];          if(r.location != nsnotfound)         {             //delete characters in range             [s1 deletecharactersinrange:r];         }         else         {             //break loop if sub string not found there no more recurrence.             break;         }     }     return s1; }  int main(int argc, char *argv[]) {      @autoreleasepool {         //initiating str1 here         nsmutablestring* str1 = [[nsmutablestring alloc] init];         [str1 appendstring:@"fhsdaihati"];          nslog(@"loc b:%@ ",deletewithsubstring(str1,@"ha"));     }     return 0; } 

Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -