0

我写了一个代码来替换删除NSString中的子字符串......我的逻辑是使用'rangeofString'来查找每个子字符串字符的位置,然后使用'ReplaceOccurance'用空格替换子字符串字符,然后修剪空白字符. 但是在 replaceOccurance 行中出现错误

*** Terminating app due to 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 an exception....

这是我的代码。

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

实际上我解决了范围的问题..因为重写代码我误认为 seocond 参数不是上限而是长度..应该如下

   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;
    }
4

1 回答 1

0

试试下面的代码,

NSString* deleteWithSubstring(NSMutableString *s1,NSString *sub)
{

    NSRange r;
    for(int i = 0; i < [s1 length]; i++)
    {
        //findout the range with "sub" string
        r = [s1 rangeOfString:sub options:NSCaseInsensitiveSearch];

        if(r.location != NSNotFound)
        {
            //Delete the characters in that range
            [s1 deleteCharactersInRange:r];
        }
        else
        {
            //break the loop if sub string not found as there is 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;
}
于 2013-08-08T08:40:37.847 回答