从字符串中提取所需的字符串

时间:2014-08-20 07:03:21

标签: ios nsstring

嘿,我是ios的新手,我想从字符串中获取字符串的一部分,

原始字符串是,

OriginalString = @" =的access_token&CAAEW97M253kB2BZBg9VysMCkOyjDi6HtvLcqtkh3Xp5y4ZBZCExq8ZCCZCKo2QZAJ8lSQ9EvOhtPPxr0pLM5NgNE5jFC1blzn8tu0xhvOcXIn放大器;期满= 5178989&#34 ;;

我想得到"& expires = 5178989"此

2 个答案:

答案 0 :(得分:0)

如果它始终是这种格式,您可以这样做:

NSArray *components = [originalString componentsSeparatedByString:@"&"];

你的字符串将是第二个对象。检索它:

NSSTring *expires = [components lastObject]

答案 1 :(得分:0)

- (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);