[英]How to trim to URL in NSString?
在調試時,我以這種方式獲得了url:
myURL __NSCFString * @"(\n "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"\n)" 0x0a8a3510
我試圖通過方法將(\\n
和\\n)
替換為""
tringByReplacingOccurrencesOfString:@" (\n " withString:@""
終於我以這種方式獲得了我的網址
myURL __NSCFString * @" "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png"" 0x08c12540
但是我想要的是
myURL __NSCFString * @"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png" 0x08c12540
有什么好的方法可以幫助我獲得想要的字符串?
這應該從字符串的末尾修剪非URL字符,並加上括號字符:
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];
如果實際上是反斜杠+ n而不是換行符,則可能需要在去除“ \\ n”之后執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.