Ios 如何查找引号之间所有字符串的所有范围
我已经研究这个问题好几天了,但找不到解决办法。我有一个很长的字符串,其中包含很多引号,我希望能够找到所有字符串(这些字符串的范围),以便使用Ios 如何查找引号之间所有字符串的所有范围,ios,objective-c,nsattributedstring,nsrange,Ios,Objective C,Nsattributedstring,Nsrange,我已经研究这个问题好几天了,但找不到解决办法。我有一个很长的字符串,其中包含很多引号,我希望能够找到所有字符串(这些字符串的范围),以便使用NSMutableAttributedString 范例 这是一个字符串“带引号”文本,还有“更多”引号文本。 我希望能够将此字符串转换为以下内容: 这是一个带引号的字符串,还有一些带引号的文本 这是我到目前为止所做的,但它不会完成字符串的其余部分: - (void)stringBetweenString:(NSString*)start andSt
NSMutableAttributedString
范例
这是一个字符串“带引号”文本,还有“更多”引号文本。
我希望能够将此字符串转换为以下内容:
这是一个带引号的字符串,还有一些带引号的文本
这是我到目前为止所做的,但它不会完成字符串的其余部分:
- (void)stringBetweenString:(NSString*)start andString:(NSString*)end inString:(NSMutableAttributedString *)text
{
NSRange startRange = [[text string] rangeOfString:start];
if (startRange.location != NSNotFound)
{
NSRange targetRange;
targetRange.location = startRange.location + startRange.length;
targetRange.length = [text length] - targetRange.location;
NSRange endRange = [[text string] rangeOfString:end options:0 range:targetRange];
if (endRange.location != NSNotFound)
{
targetRange.length = endRange.location - targetRange.location;
[text addAttribute:NSFontAttributeName value:[NSFont fontWithName:@"Helvetica-Bold" size:12.0] range:targetRange];
}
}
}
如果文本太长,这可能会有点慢,但它的作品。处理正则表达式解决方案
NSString *string = @"Example This is a string \"with quoted1\" text and there is \"some more1\" quoted text. I want to be able to turn this string into the following: This is a string \"with quoted2\" text and there is \"some more2\" quoted text.";
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:nil];
int leftFromLeft = 0;
while ([string rangeOfString:@"\""].location != NSNotFound) {
NSRange quoteLocationFirst = [string
rangeOfString:@"\""
options:0
range:NSMakeRange(leftFromLeft, string.length - leftFromLeft)
];
leftFromLeft = quoteLocationFirst.location + quoteLocationFirst.length;
NSRange quoteLocationSecond = [string
rangeOfString:@"\""
options:0
range:NSMakeRange(leftFromLeft, string.length - leftFromLeft)
];
NSRange quotedTextRange = NSMakeRange(
quoteLocationFirst.location,
quoteLocationSecond.location - quoteLocationFirst.location + 1
);
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:quotedTextRange];
NSLog(@"%@ \r\n\r\n", [string substringWithRange:quotedTextRange]);
leftFromLeft = quoteLocationSecond.location + quoteLocationSecond.length;
if ([string rangeOfString:@"\"" options:0 range:NSMakeRange(leftFromLeft, string.length - leftFromLeft)].location == NSNotFound) {
string = @"";
}
}
编辑
正则表达式解决方案似乎更好/更快
NSString *string = @"Example This is a string \"with quoted1\" text and there is \"some more1\" quoted text. I want to be able to turn this string into the following: This is a string \"with quoted2\" text and there is \"some more2\" quoted text. Example This is a string \"with quoted3\" text and there is \"some more3\" quoted text. I want to be able to turn this string into the following: This is a string \"with quoted4\" text and there is \"some more4\" quoted text.";
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:string attributes:nil];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\"([^\"]*)\"" options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)];
for (NSTextCheckingResult *match in arrayOfAllMatches) {
UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:match.range];
//NSLog(@"%@", [string substringWithRange:match.range]);
}
你听说过NSRegularExpression吗?你就是那个人!Regex做得好多了谢谢!