Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove part of an NSString

I have an NSString as follows:

<img alt="996453912" src="http://d2gg0uigdtw9zz.cloudfront.net/large/996453912.jpg" /><a href="http://www.dealcatcher.com/shop4tech-coupons">Shop4Tech Coupons</a>

I only need the first part (before the <a href part), and I cannot figure out how to remove the second part.

I have tried a ton, but it has not worked.

like image 749
Michael Amici Avatar asked Dec 12 '10 18:12

Michael Amici


1 Answers

Use something like:

NSRange rangeOfSubstring = [string rangeOfString:@"<a href"];

if(rangeOfSubstring.location == NSNotFound)
{
     // error condition — the text '<a href' wasn't in 'string'
}

// return only that portion of 'string' up to where '<a href' was found
return [string substringToIndex:rangeOfSubstring.location];

So the two relevant methods are substringToIndex: and rangeOfString:.

like image 180
Tommy Avatar answered Sep 19 '22 01:09

Tommy