English 中文(简体)
• 在引言之间架设NSRange?
原标题:Getting NSRange of string between quotations?

I have a string:

He said "hello mate" yesterday.

我想从第一个引言到最后的引言,获得一个NSRange。 因此,我试图这样做:

NSRange openingRange = [title rangeOfString:@"""];
NSRange closingRange = [title rangeOfString:@"""];
NSRange textRange = NSMakeRange(openingRange.location, closingRange.location+1 - openingRange.location);

But I m not sure how to make it distinguish between the first quote and the second quote. How would I do this?

最佳回答

您可以定期表达:

NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(["])(?:\\\1|.)*?\1" options:0 error:&error];

NSRange range = [regex rangeOfFirstMatchInString:myString options:0 range:NSRangeMake(0, [myString length]];

Don forget 检查错误;

问题回答

你们总是能够使用范围 联系:选择:安排:第二个选择(从第一个选择的位置开始)。

Option 1

- (NSRange)rangeOfQuoteInString:(NSString *)str {
    int firstMatch = [str rangeOfString:@"""].location;
    int secondMatch = [str rangeOfString:@""" options:0 range:NSMakeRange(firstMatch + 1, [str length] - firstMatch - 1)].location;
    return NSMakeRange(firstMatch, secondMatch + 1 - firstMatch);
}

我希望这是正确的。 在晚宴上打电话;

另一回事是,由于范围可能相似,为什么在座标和寻找对应点时不会篡改果价值;第2号? 可能快或更快。

Option 2

- (NSRange)rangeOfQuoteInString:(NSString *)str {
    int firstMatch = -1;
    int secondMatch = -1;
    for (int i = 0; i < [str length]; i = i + 1) {
        unichar c = [str characterAtIndex:i];
        if (c ==  " ) {
            if (firstMatch == -1) {
                firstMatch = i;
            } else {
                secondMatch = i;
                break;
            }
        }
    }
    if (firstMatch == -1 || secondMatch == -1) {
        // No full quote was found
        return NSMakeRange(NSNotFound, 0);
    } else {
        return NSMakeRange(firstMatch, secondMatch + 1 - firstMatch);
    }
}




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签