0

can someone please help me out writing this code in objective C :

function extractStringFromString ($string, $start, $end) {

    $startPos = strpos($string,$start);
    $stringEndTagPos = strpos($string,$end,$startPos);
    $stringBetween = substr($string,$startPos+strlen($start),$stringEndTagPos-$startPos-strlen($start));

    if (strlen($stringBetween) != 0) {

        return $stringBetween;
        return true;
    }
    else {

        return false;
    }

}

what the function does is simple, takes 3 values, $string which is a text and $start and $end which the function will look for in the text ($start will be the start of the search, and once start is found, $end will be the end of the search and it will return the value)

Thanks

2 Answers 2

4
-(NSString*)extractStringFromString:(NSString*)text fromIndex(NSInteger)start toIndex:(NSInteger)end{
    NSRange cropRange = {start, [text length] - end};
    return [text substringWithRange: cropRange];

}

But of course you should really just use:

    NSRange cropRange = {aStartIndex, aLength};
    NSString* substring = [text substringWithRange: cropRange];
Sign up to request clarification or add additional context in comments.

1 Comment

They are not equivalent. You are not searching for substring indexes.
2

Searches first start string and last end string

- (NSString *)extractStringFromString:(NSString *)string start:(NSString *)start end:(NSString *)end
{
    NSRange indexStart = [string rangeOfString:start];
    if (indexStart.location == NSNotFound) return nil;

    NSRange searchRange = NSMakeRange(indexStart.location + indexStart.length, [string length] - indexStart.location - indexStart.length);
    NSRange indexEnd = [string rangeOfString:end options:NSBackwardsSearch range:searchRange];
    if (indexEnd.location == NSNotFound) return nil;

    return [string substringWithRange:NSMakeRange(indexStart.location + indexStart.length, indexEnd.location - indexStart.location - indexStart.length)];
}

Searches first start string and first end string after first start string:

- (NSString *)extractStringFromString:(NSString *)string start:(NSString *)start end:(NSString *)end
{
    NSRange indexStart = [string rangeOfString:start];
    if (indexStart.location == NSNotFound) return nil;

    NSRange searchRange = NSMakeRange(indexStart.location + indexStart.length, [string length] - indexStart.location - indexStart.length);
    NSRange indexEnd = [string rangeOfString:end options:NSLiteralSearch range:searchRange];
    if (indexEnd.location == NSNotFound) return nil;

    return [string substringWithRange:NSMakeRange(indexStart.location + indexStart.length, indexEnd.location - indexStart.location - indexStart.length)];
}

6 Comments

thanks alot man, the line "return [string..." returns an error (program received signal "sigabrt") , any idea why?? :)
Give me, please, the example of your call. (input strings)
this is the code : NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString: @"radarurl.com/url/twtio.com"]]; NSString *siteID = [self extractStringFromString:html start:@"var SITE_ID = " end:@";"];
hey thanks, now it doesnt return an error but it doesnt return the right string, it actually returns the entire html page (if you test it on xcode)
Check your test data. I've tested it and it works fine. Use NSLog() for checking values of all input strings.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.