从NSString获取子字符串,直到到达特定单词 [英] Get a substring from an NSString until arriving to a specific word
本文介绍了从NSString获取子字符串,直到到达特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们说我有 NSString
: @国家地址电话:号码
。
如何获取 Tel
之前的子字符串? (国家地址)
然后我如何才能获得 Tel
之后的子字符串? (编号)
Let us say I have this NSString
: @"Country Address Tel:number"
.
How can I do to get the substring that is before Tel
? (Country Address )
And then How can I do to get the substring that is after Tel
? (number)
推荐答案
使用NSScanner:
Use NSScanner:
NSString *string = @"Country Address Tel:number";
NSString *match = @"tel:";
NSString *preTel;
NSString *postTel;
NSScanner *scanner = [NSScanner scannerWithString:string];
[scanner scanUpToString:match intoString:&preTel];
[scanner scanString:match intoString:nil];
postTel = [string substringFromIndex:scanner.scanLocation];
NSLog(@"preTel: %@", preTel);
NSLog(@"postTel: %@", postTel);
NSLog输出:
preTel:国家地址
postTel:number
preTel: Country Address
postTel: number
这篇关于从NSString获取子字符串,直到到达特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文