【问题标题】:XCode: How to handle Space character in string? [duplicate]XCode:如何处理字符串中的空格字符? [复制]
【发布时间】:2013-06-21 19:17:20
【问题描述】:

我想问是否要创建一个NSString,比如

for ( int i=1; i<=10; i++) {
    NSString *picURLstring = [NSString stringWithFormat:@"http://localhost/Testing/files/Demo Presentation/Slide%d.JPG", i] ;
...
}

这是我的代码的一部分,以便我以后可以获取 10 张图片的 URL。然后我使用这个 URL 从我的服务器下载图像,但它似乎不起作用。但是,当我将服务器中的文件从“Demo Presentation”更改为“DemoPresentation”时,Xcode 语句也改为:

    for ( int i=1; i<=10; i++) {
    NSString *picURLstring = [NSString stringWithFormat:@"http://localhost/Testing/files/DemoPresentation/Slide%d.JPG", i] ;
...
}

有效!它可以从我的服务器下载我需要的图像。那么请问如何在字符串中实现空格?我尝试使用 %20,因此在字符串中使用“Demo%20Presentation”,但它仍然无法正常工作,请帮助。

【问题讨论】:

  • 你能详细说明哪里出了问题吗? “我的测试后没有得到任何字符串”是什么意思?
  • 以后这个字符串怎么用?
  • NSString *encodedString = [myString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];像这样尝试对于您的情况..... for (int i=1; ilocalhost/Testing/files/Demo Presentation/Slide%d.JPG", i ] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; }
  • 抱歉我的英语不好,我编辑了问题以详细说明错误。
  • @ChowCena 你试过我的评论吗

标签: objective-c cocoa


【解决方案1】:

试试这个:

for ( int i=1; i<=10; i++) {
    NSString *picURLstring = [NSString stringWithFormat:@"http://localhost/Testing/files/Demo Presentation/Slide%d.JPG", i] ;
    NSURL *url = [NSURL URLWithString:[picURLstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    //use url ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2021-07-11
    • 1970-01-01
    • 2011-04-17
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多