url链接之间的空格

时间:2014-05-15 09:59:05

标签: ios objective-c uiimageview nsstring

NSString* str = [inventoryDetails objectAtIndex:i];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(j, 10, 280, 180)];

if (![str isEqual:[NSNull null]]) {
    NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:[[inventoryDetails objectAtIndex:i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    UIImage *img=[[UIImage alloc]initWithData:data];

我的输出为

  

的https:google.com/images/320.png

如何删除网址中的空格

3 个答案:

答案 0 :(得分:4)

试试这个

NSString *str = @"http://...";
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

答案 1 :(得分:2)

如果你的空间位于两侧,你可以修剪你的NSString。

NSString *trimMe = @"    Trim this string      ";
trimMe = [trimMe stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"Trimmed String: ++%@++", trimMe);
//returnes Trimmed String: ++Trim this string+++

答案 2 :(得分:2)

只是做:

[str stringByReplacingOccurrencesOfString:@" " withString:@""]