Search code examples
htmliosuiwebview

How to delete a substring from NSString from a specific position?


I want to set UIWebView with htmlstring and I have problem with an image: ...

<img align="absBottom" alt="" height="350" src="http://www.xyz.com//files/userfiles/images/abc.jpg" vspace="10" width="625" />

...

I want to replace height and width to 200 and 320. I'm using

htmlString = [htmlString stringByReplacingOccurrencesOfString:@"width=\"" withString:@"width=\"320]; 

then I've got result :

<img align="absBottom" alt="" height="350" src="http://www.xyz.com//files/userfiles/images/abc.jpg" vspace="10" width="320625" />

How to delete the number after the width from htmlString or what is the right way to replace the width to 320px?


Solution

  • Try this using NSScanner:

    NSScanner *theScanner;
    NSString *subStrng =nil;
    theScanner = [NSScanner scannerWithString:htmlString];
    [theScanner scanUpToString:@"width" intoString:NULL] ;
    [theScanner scanUpToString:@" " intoString:&subStrng] ;
    htmlString = [htmlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@", subStrng] withString::@"width=\"320\""];
    NSLog("%@",subStrng);