Search code examples
iosobjective-cxcodewebviewnsurl

Remove all text after a symbol - iOS


I have a web view which encodes the string which is turned into url and when I check the url in the NSLog - this is the url.

http://www.bbc.co.uk/news/technology-30447248%23sa-ns_mchannel=rss&ns_source=PublicRSS20-sa%20%20%20%20%20%20%20%20

I made this scanner below and I want to delete everything after the FIRST % so basically %23 is the first one I want to scan from % to the end of string.

 NSString *webString22222 = n2;



    NSScanner *stringScanner22222 = [NSScanner scannerWithString:webString22222];



    NSString *content22222 = [[NSString alloc] init];





    [stringScanner22222 scanUpToString:@"%" intoString:Nil];



    [stringScanner22222 scanUpToString:@"" intoString:&content22222];

Scanner I created, as you can see it has a percent sign for first scanUpToString then the second one I want some code which will indicate end of string like to be more precise something similar to this in real programming scanUpToString:webString22222.endOfString something to scan up to the end of the string and then delete it.

Sorry for not providing code earlier. Thanks.


Solution

  • Try this I hope it may help you.
    
     NSString *str1 = @"http://www.bbc.co.uk/news/technology-30447248%23sa-ns_mchannel=rss&ns_source=PublicRSS20-sa%20%20%20%20%20%20%20%20";
        NSRange range = [str1 rangeOfString:@"sa"];
    
        NSString *newString = [str1 substringToIndex:range.location];
        NSLog(@"%@",newString);