-1

我正在尝试从字符串中获取 SubString

这是我正在调用的方法

-(NSString *)subStringBetweenStrings:(NSString*)str open:(NSString*)open close:(NSString *)close
{
    if ( str == nil || open == nil || close == nil )
        return nil;

    int start = [str rangeOfString:open].location;
    if (start != -1)
    {
        int end = [str rangeOfString:close].location;
        if ( end != -1)
        {
            return [str substringFromIndex:start]; // DONT WANT
        }
    }
    return nil;       
}

我有start point,我有End point。如何从开始和结束索引中获取子字符串,例如 return[ str subStringWithRange:(start, end)];

4

1 回答 1

5

挑剔...

  • 替换int startNSUIntegerNSRange location就是这样定义的,
  • 不要比较start-1而是要NSNotFound

... 回答 ...

return [str substringWithRange:NSMakeRange( start, end - start + 1 )]

于 2012-11-09T17:46:51.793 回答