无法替换NSMutableString中的字符串

时间:2011-12-16 20:04:16

标签: iphone objective-c ios

这是我的字符串:

2011-10-07T08:55:16-05:00

我试图用以下代码删除冒号:

NSRange range = NSMakeRange(dateString.length-3, 1);
NSString *temp = [dateString substringWithRange:range];
if ([temp isEqualToString:@":"])
    [dateString replaceCharactersInRange:range withString:@""];

我的代码输入if语句,所以我知道它找到了冒号。但它崩溃了,最后一行没有错误。我做错了什么?

1 个答案:

答案 0 :(得分:0)

试试这个:

NSMutableString *dateString = [NSMutableString stringWithString:@"2011-10-07T08:55:16-05:00"];

NSRange range = NSMakeRange(dateString.length-3, 1);
NSString *temp = [dateString substringWithRange:range];
if ([temp isEqualToString:@":"])
    [dateString replaceCharactersInRange:range withString:@""];