我有一个如下所示的字符串。
<br><br><br><br><br> SomeHtmlString <br><br><br><br><br>
我想删除 br 标签,例如在 SomeHtmlString 中保留中间 br 标签的修剪函数。有什么功能可以很快做到这一点吗?
例如
<br><br><br>test1<br><br>test2<br><br><br><br>
到
test1<br><br>test2
我有一个如下所示的字符串。
<br><br><br><br><br> SomeHtmlString <br><br><br><br><br>
我想删除 br 标签,例如在 SomeHtmlString 中保留中间 br 标签的修剪函数。有什么功能可以很快做到这一点吗?
例如
<br><br><br>test1<br><br>test2<br><br><br><br>
到
test1<br><br>test2
这是使用正则表达式的方法。它一次只匹配一个
,并在字符串结尾的开头替换它。
NSMutableString *replaceMe = [[NSMutableString alloc ]
initWithString:@"<br><br > <br > test<br>test2<br><br>"];
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"^ *<br *> *"
options:NSRegularExpressionCaseInsensitive
error:&error];
do {
;
} while ([regex replaceMatchesInString:replaceMe options:NSMatchingCompleted range:NSMakeRange(0, replaceMe.length) withTemplate:@""] != 0);
regex = [NSRegularExpression
regularExpressionWithPattern:@" *<br *> *$"
options:NSRegularExpressionCaseInsensitive
error:&error];
do {
;
} while ([regex replaceMatchesInString:replaceMe options:NSMatchingCompleted range:NSMakeRange(0, replaceMe.length) withTemplate:@""] != 0);
NSLog(@"string=%@", replaceMe);
and that does strip "<br><br > <br > test<br>test2<br><br>"
down to test<br>test2
.
It's probably not the neatest solution but it is very easy to modify to match different expressions, with different whitespace, for example.
It's also possible to use the regular expressions to match several <br>
s in one go:
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@"^ *(<br *> *)+"
options:NSRegularExpressionCaseInsensitive
error:&error];
[regex replaceMatchesInString:replaceMe options:NSMatchingCompleted range:NSMakeRange(0, replaceMe.length) withTemplate:@""];
NSRegularExpression *regex = [NSRegularExpression
regularExpressionWithPattern:@" *(<br *> *)+$"
options:NSRegularExpressionCaseInsensitive
error:&error];
[regex replaceMatchesInString:replaceMe options:NSMatchingCompleted range:NSMakeRange(0, replaceMe.length) withTemplate:@""];
which avoids the looping but is a little harder to modify.
你可以这样做:
NSString* htmlString= @"<br><br><br><br><br> SomeHtmlString <br><br><br><br><br>";
NSString* pureString= [htmlString stringByReplacingOccurrencesOfString: @"<br>" withString: @""];
所以你会在 pureString 中有 @" SomeHtmlString "。
您可以使用它来去除不需要的位:
[yourString stringByReplacingOccurrencesOfString:@"<br>" withString:@""];
然后你会使用这样的东西来按照你想要的方式重新制作你的字符串:
NSString *newString = [NSString stringWithFormat:@"<br>%@<br>", yourString];
您可能还想查看 stringByTrimmingCharactersInSet:
你可以用 NSString 做很多事情。查看类参考:https ://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html
编辑:
substringToIndex: 可能是你的朋友。您可以这样做来确定字符串的前 4 个字符是否包含要删除的字符:
NSString *subString = [yourString substringToIndex:4];
if ([subString isEqualToString:@"<br>"]) {
yourString = [yourString substringFromIndex:4];
}
然后,您将创建一个没有这 4 个字符的新字符串。您一直这样做,直到前 4 个字符不等于您要删除的字符。
您可以使用 substringFromIndex 在字符串末尾执行类似的操作。您将需要知道原始字符串的长度,以确保您的子字符串都不会超出范围。
Alternative regular expression rendition:
NSString *input = @"<br><br><br><br><br><br>test<br>test2<br><br><br><br><br><br><br><br><br><br>";
__block NSString *output;
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^(<br>)*(.*?)(<br>)*$"
options:NSRegularExpressionCaseInsensitive
error:&error];
[regex enumerateMatchesInString:input
options:0
range:NSMakeRange(0, [input length])
usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange matchRange = [result rangeAtIndex:2];
output = [input substringWithRange:matchRange];
}];
if (output)
NSLog(@"Found: %@", output);