iphone - 从Iphone SDK中的字符串中删除最后一个“/”

标签 iphone objective-c ios xcode nsstring

我有一个像这样的字符串:

http://feedproxy.google.com/~r/MakePartsFast/~3/1dZEOLADapg/

我想使用此字符串加载html页面。但是因为最后在字符串中出现“/”,所以我得到的URL为空。

我的代码如下:

NSURL *ur=[NSURL URLWithString:Mystring];
        NSLog(@"%@",ur); //getting null value

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:ur];

        NSLog(@"%@",requestObj);

因此,如何从字符串中删除最后一个“/”?

最佳答案

试试这个代码...

    NSString *Mystring = @"http://feedproxy.google.com/~r/MakePartsFast/~3/1dZEOLADapg/";
    if ([Mystring isEqualToString:@""]||[Mystring isEqual:nil]) {

        NSLog(@"\n\n String is NULL");
    }
    else {
        NSString *TempURl = [Mystring substringToIndex:[Mystring length]-1];

        NSString *urlstr =[TempURl stringByReplacingOccurrencesOfString:@" " withString:@""];
        urlstr =[urlstr stringByReplacingOccurrencesOfString:@"\n" withString:@""];

        urlstr =[urlstr stringByReplacingOccurrencesOfString:@"\t" withString:@""];
        [urlstr retain];

        NSURL *ur=[NSURL URLWithString:urlstr];
        NSLog(@"%@",ur); //now get string value here

        //        NSURLRequest *requestObj = [NSURLRequest requestWithURL:ur];
        //        
        //        NSLog(@"%@",requestObj);
    }

我已注销: http://feedproxy.google.com/~r/MakePartsFast/~3/1dZEOLADapg

希望这对您有帮助...

关于iphone - 从Iphone SDK中的字符串中删除最后一个“/”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13973056/

相关文章:

iphone - 使用 git 管理 phonegap/cordova 项目

iphone - 在后台运行一个进程,而用户仍然可以使用 UI

iphone - 如何使用 Objective C 在两台机器之间创建远程连接

iphone - 我尝试使用 setReturnsDistinctResults 通过 Core Data 获得不同的结果,但无法使其正常工作

jquery - 日期输入限制 iPad

ios - CGColorGetComponents() 没有为黑色和白色返回正确的值

iphone - IOS:使用图案图像作为背景 - 内存泄漏

ios - 无法重现苹果应用商店审核团队报告的问题

objective-c - NSArray 提取项目

iphone - 未调用 fbDidLogin - iOS