简体   繁体   中英

How to trim to URL in NSString?

I got my url in this way when I debug:

myURL   __NSCFString *  @"(\n    "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"\n)"  0x0a8a3510

I tried to replace (\\n and \\n) to "" by method

tringByReplacingOccurrencesOfString:@" (\n " withString:@""

Finally I got my url in this way

myURL   __NSCFString *  @"   "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"" 0x08c12540

but what I want is

myURL   __NSCFString *  @"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"  0x08c12540

Are there any good way to help me to get the string that I want?

This should trim non-URL characters from the ends of a string, plus parentheses characters:

NSMutableCharacterSet* urlSet = [[NSMutableCharacterSet alloc] init];
urlSet = [urlSet formUnionWithCharacterSet:[NSCharacterSet URLUserAllowedCharacterSet]];
urlSet = [urlSet formUnionWithCharacterSet:[NSCharacterSet URLPasswordAllowedCharacterSet]];
urlSet = [urlSet formUnionWithCharacterSet:[NSCharacterSet URLHostAllowedCharacterSet]];
urlSet = [urlSet formUnionWithCharacterSet:[NSCharacterSet URLPathAllowedCharacterSet]];
urlSet = [urlSet formUnionWithCharacterSet:[NSCharacterSet URLQueryAllowedCharacterSet]];
urlSet = [urlSet formUnionWithCharacterSet:[NSCharacterSet URLFragmentAllowedCharacterSet]];
urlSet = [[urlSet invertedSet] mutableCopy];
[urlSet addCharactersInString:@"()"];

myURL = [myURL stringByTrimmingCharactersInSet:urlSet];

You may need to do that after stripping "\\n" if it is actually a backslash+n rather than a newline char.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM