```
-(NSString *)handleStringWithString:(NSString *)str{
NSMutableString * muStr = [NSMutableString stringWithString:str];
while (1) {
NSRange range = [muStr rangeOfString:@"("];
NSRange range1 = [muStr rangeOfString:@")"];
if (range.location != NSNotFound) {
NSInteger loc = range.location;
NSInteger len = range1.location - range.location;
[muStr deleteCharactersInRange:NSMakeRange(loc, len + 1)];
}else{
break;
}
}
return muStr;
}
```
iOS 截取字符串中两个指定字符串中间的字符串方法
下面小编就为大家分享一篇iOS 截取字符串中两个指定字符串中间的字符串方法,具有很好的参考价值
例如,要截取一个字符串中,两个指定字符串中间的字符串,OC截取方法如下:
// 要截取 "> 和 </ 之间的汉字内容:
```NSString *string = @"<a href=\"http\">这是要截取的内容</a>";
NSRange startRange = [string rangeOfString:@"\">"];
NSRange endRange = [string rangeOfString:@"</"];
NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
NSString *result = [string substringWithRange:range];
NSLog(@"%@",result);
}
```
为了使用方便,我们可以给 NSString 分类添加一个截取字符创的方法,直接返回截取后的字符串:代码如下
```// NSString 分类中实现如下方法:
#import "NSString+Extension_NSString.h"
@implementation NSString(Extension_NSString)
// 截取字符串方法封装
// 截取字符串方法封装
- (NSString *)subStringFrom:(NSString *)startString to:(NSString *)endString{
NSRange startRange = [self rangeOfString:startString];
NSRange endRange = [self rangeOfString:endString];
NSRange range = NSMakeRange(startRange.location + startRange.length, endRange.location - startRange.location - startRange.length);
return [self substringWithRange:range];
}
@end
```
// 控制器中直接用字符串调用:
```#import "ViewController.h"
#import "NSString+Extension_NSString.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *string = @"<a href=\"http\">这是要截取的内容</a>";
NSString *result = [string subStringFrom:@"\">" to:@"</"];
NSLog(@"%@",result);
}
```
// 这样使用起来更方便,简洁
以上这篇iOS 截取字符串中两个指定字符串中间的字符串方法
网友评论