ios - Check NSString is encoded or not -


how detect nsstring encoded or not.?

i'm encoding string below. before encoding want verify weather [product url] encoded or not.

nsstring *encodedurlstring=[[product url] stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; 

you try decoding string , see if original string , decoded string same or not. if same wasn't encoded yet.

nsstring *original = product.url; nsstring *decoded = [original stringbyreplacingpercentescapesusingencoding:nsutf8encoding]; if ([original isequaltostring:decoded]) {     // url not encoded yet     nsstring *encodedurlstring=[[product url] stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; } else {     // url encoded } 

btw - these methods deprecated of ios 9 if app's deployment target ios 9.0 or later should use newer methods.

for ios 9 or later should use:

nsstring *original = product.url; nsstring *decoded = [original stringbyremovingpercentencoding]; if ([original isequaltostring:decoded]) {     // url not encoded yet     nsstring *encodedurlstring=[[product url] stringbyaddingpercentencodingwithallowedcharacters:[nscharacterset alphanumericcharacterset]]; } else {     // url encoded } 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -