Iphone 即使NSString Replace中的范围正确,NSRANGE异常
我已经编写了一个代码来替换删除NSString中的子字符串…我的逻辑是使用“rangeofString”来查找每个子字符串字符的位置,然后使用“ReplaceOccurance”以空格替换子字符串字符,然后修剪空白字符。但是我在替换行中得到一个错误Iphone 即使NSString Replace中的范围正确,NSRANGE异常,iphone,xcode,nsstring,nsrangeexception,Iphone,Xcode,Nsstring,Nsrangeexception,我已经编写了一个代码来替换删除NSString中的子字符串…我的逻辑是使用“rangeofString”来查找每个子字符串字符的位置,然后使用“ReplaceOccurance”以空格替换子字符串字符,然后修剪空白字符。但是我在替换行中得到一个错误 *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString replaceOccurrencesOfString:withStri
*** 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;
}
NSString*deleteWithSubstring(NSString*s1,NSString*sub)
{
长整型长度=[子长度],范围=0;
NSRange;
对于(int i=0;i请尝试以下代码
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;
}
NSString*deleteWithSubstring(NSMutableString*s1,NSString*sub)
{
NSRange;
对于(int i=0;i<[s1长度];i++)
{
//用“sub”字符串查找范围
r=[s1 rangeOfString:子选项:NSCaseInsensitiveSearch];
if(r.location!=NSNotFound)
{
//删除该范围内的字符
[s1 deleteCharactersRange:r];
}
其他的
{
//如果未找到子字符串,则中断循环,因为不再重复。
打破
}
}
返回s1;
}
int main(int argc,char*argv[])
{
@自动释放池{
//在这里启动str1
NSMutableString*str1=[[NSMutableString alloc]init];
[str1 appendString:@“fHsdaihati”];
NSLog(@“Loc B:%@”,删除子字符串(str1,@“ha”);
}
返回0;
}
我已经找到了解决方案..以上解释感谢您的帮助:-)使用您当前的代码,您想做什么?只是为了消除崩溃问题?还是从str1中删除子字符串?如果我将fHsdaihati作为str1传递,我从deleteWithSubstring方法中获得“fsdihati”。这是您想要的结果吗?因为仍然存在“ha”它上的子字符串(fsdihati)
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;
}