extract substring from NSString [closed]

ぃ、小莉子 提交于 2019-12-23 09:15:09

问题


I wants to extract substring from NSString. How it is possible.

Example : String is abcdefwww.google.comabcdef

output : www.google.com

In the main String, It is not predefined which url comes, It may be google.com or yahoo.com etc.

So I have to find the Start location of w in www and the the last location of m in .com.

So my question is that how I can find the start location of w in www and the location of m in .com, so i extract this part

Edited due to issue :

How we can extract www.mywebsite.co.uk from this string asdfwww.mywebsite.co.ukasdfajsf

Thanks


回答1:


substring from string like bellow

       - (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];
    }

use this method like below...

NSString *strTemp = [self extractString:@"abcdefwww.google.comabcdef" toLookFor:@"www" skipForwardX:0 toStopBefore:@".com"];
NSLog(@"\n\n Substring ==>> %@",strTemp);

for more information see bellow link..

help-needed-function-extract-string-string




回答2:


You can get substring using following code

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

To call this,

NSString *cURL=[self stringBetweenString:@"www." andString:@".com" withstring:@"abcdefwww.google.comabcdef"];

NSString *cAudiolink=[NSString stringWithFormat:@"www.%@.com",cURL];

cAudiolink is the resulted string




回答3:


I got the solution of my problem, after googling. I implement the code below and it's work fine for me

   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



回答4:


You can use a regex.

Using this category:

@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

You can call:

[@"abcdefwww.google.comabcdef" firstMatchWithRegex:@"(www.*\\.com)" error:nil]

which returns:

www.google.com


来源:https://stackoverflow.com/questions/13415776/extract-substring-from-nsstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!