iOS get/delete parameters in URL

Source: Internet
Author: User

1. Get a parameter in the URL:

-(NSString *) GetParameter: (NSString *) parameter urlstr: (NSString *) URL {nserror*error; if(!URL) {                return@""; } nsstring*regtags = [[NSString alloc] Initwithformat:@"(^|&|\\?) +%@=+ ([^&]*) (&|$)", parameter]; Nsregularexpression*regex = [nsregularexpression regularexpressionwithpattern:regtags options:nsregularexpressioncaseinsensitive Error :&ERROR]; Nsarray*matches = [Regex matchesinstring:url options:0Range:nsmakerange (0, [url length])];  for(Nstextcheckingresult *matchinchmatches) {NSString*tagvalue = [url substringwithrange:[match rangeatindex:2]];//the string corresponding to group 2                returnTagvalue; }        return@""; }

2. Delete a parameter in the URL:

-(NSString *) Deleteparameter: (NSString *) parameter withoriginurl: (NSString *) originurl {nsstring*FINALSTR = [NSStringstring]; Nsmutablestring* Mutstr =[nsmutablestring Stringwithstring:originurl]; Nsarray*strarray =[Mutstr Componentsseparatedbystring:parameter]; Nsmutablestring*FIRSTSTR = [Strarray objectatindex:0]; Nsmutablestring*laststr =[Strarray Lastobject]; Nsrange CharacterRange= [Laststr rangeofstring:@"&"]; if(Characterrange.location! =nsnotfound) {Nsarray*lastarray = [Laststr componentsseparatedbystring:@"&"]; Nsmutablearray*mutarray =[Nsmutablearray Arraywitharray:lastarray]; [Mutarray Removeobjectatindex:0]; NSString*MODIFIEDSTR = [Mutarray componentsjoinedbystring:@"&"]; Finalstr= [[Strarray objectatindex:0]STRINGBYAPPENDINGSTRING:MODIFIEDSTR]; } Else {                //end With '? ', ' & 'FINALSTR = [firststr substringtoindex:[firststr length]-1]; }    returnFinalstr; }

iOS get/delete parameters in URL

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.