从NSString中提取子string
我想从NSString中提取子string。 怎么可能。
示例:string是abcdefwww.google.comabcdef
输出: www.google.com
在主要string中,不会预定义哪个url来,可能是google.com或yahoo.com等。
所以我必须在www中findw的起始位置,在.com中findm的最后一个位置。
所以我的问题是,我怎样才能findWW的起始位置和在.com中的m的位置,所以我提取这部分
由于问题编辑:
我们如何从这个string中提取www.mywebsite.co.uk asdfwww.mywebsite.co.ukasdfajsf
谢谢
string的子串像波纹pipe
- (NSString *)extractString:(NSString *)fullString toLookFor:(NSString *)lookFor skipForwardX:(NSInteger)skipForward toStopBefore:(NSString *)stopBefore { NSRange firstRange = [fullString rangeOfString:lookFor]; NSRange secondRange = [[fullString substringFromIndex:firstRange.location + skipForward] rangeOfString:stopBefore]; NSRange finalRange = NSMakeRange(firstRange.location + skipForward, secondRange.location + [stopBefore length]); return [fullString substringWithRange:finalRange]; }
使用下面的这个方法…
NSString *strTemp = [self extractString:@"abcdefwww.google.comabcdef" toLookFor:@"www" skipForwardX:0 toStopBefore:@".com"]; NSLog(@"\n\n Substring ==>> %@",strTemp);
欲了解更多信息请参阅下面的链接..
帮助需要的function提取物串,串
你可以使用下面的代码获取子string
-(NSString*)stringBetweenString:(NSString*)start andString:(NSString *)end withstring:(NSString*)str { NSScanner* scanner = [NSScanner scannerWithString:str]; [scanner setCharactersToBeSkipped:nil]; [scanner scanUpToString:start intoString:NULL]; if ([scanner scanString:start intoString:NULL]) { NSString* result = nil; if ([scanner scanUpToString:end intoString:&result]) { return result; } } return nil; }
为了调用这个,
NSString *cURL=[self stringBetweenString:@"www." andString:@".com" withstring:@"abcdefwww.google.comabcdef"]; NSString *cAudiolink=[NSString stringWithFormat:@"www.%@.com",cURL];
cAudiolink是结果string
Googlesearch后,我得到了我的问题的解决scheme。 我执行下面的代码,它对我来说工作正常
NSString *finalURL; NSString *string=@"abcdefwww.yahoomail.comabcdef"; NSRange range = [string rangeOfString:@"www"]; NSString *tempUrl=[string substringFromIndex:range.location]; NSRange range2 = [tempUrl rangeOfString:@".com"]; finalURL=[tempUrl substringWithRange:NSMakeRange(0, range2.location+4)]; NSLog(@"your URL is %@",finalURL); //prints www.yahoomail.com
你可以使用正则expression式。
使用这个类别:
@implementation NSString (MyRegex) - (NSString *)firstMatchWithRegex:(NSString *)regex error:(NSError **)e { NSError *error = nil; NSRegularExpression *re = [NSRegularExpression regularExpressionWithPattern:regex options:NSRegularExpressionSearch error:&error]; if(re == nil) { if(e) *e = error; return nil; } NSArray *matches = [re matchesInString:self options:0 range:NSMakeRange(0, [self length])]; if([matches count] == 0) { NSString *errorDescription = [NSString stringWithFormat:@"Can't find a match for regex: %@", regex]; NSError *error = [NSError errorWithDomain:NSStringFromClass([self class]) code:0 userInfo:@{NSLocalizedDescriptionKey : errorDescription}]; if(e) *e = error; return nil; } NSTextCheckingResult *match = [matches lastObject]; NSRange matchRange = [match rangeAtIndex:1]; return [self substringWithRange:matchRange]; } @end
你可以打电话给:
[@"abcdefwww.google.comabcdef" firstMatchWithRegex:@"(www.*\\.com)" error:nil]
它返回:
www.google.com