user1673099
user1673099

Reputation: 3289

Remove last "/" from string in Iphone SDK

I have a string like:

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

I want to use this string to load html page. But because of "/" at last in string, I get the URL is as null.

my code is as follow:

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

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:ur];

        NSLog(@"%@",requestObj);

So, How can i remove this last "/" from string??

Upvotes: 1

Views: 919

Answers (5)

Paras Joshi
Paras Joshi

Reputation: 20541

try this code ...

    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);
    }

I Got Log OutPut : http://feedproxy.google.com/~r/MakePartsFast/~3/1dZEOLADapg

hope this help you...

Upvotes: 3

Roland Keesom
Roland Keesom

Reputation: 8288

I think you need to encode the string like:

NSString *encodedString = [Mystring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Upvotes: 1

ask4asif
ask4asif

Reputation: 676

If you are sure that you need to remove the last character from string in every case, then you can get a newString as

NSString *newString = [oldString substringToIndex:[oldString length]-1];

Upvotes: 1

Mouhamad Lamaa
Mouhamad Lamaa

Reputation: 884

try this may help you

NSString * Mystring=@"http://feedproxy.google.com/~r/MakePartsFast/~3/1dZEOLADapg/";
[alfa substringToIndex:alfa.length-1];

Upvotes: 0

Ismael
Ismael

Reputation: 3937

Are you sure it's because of the last '/'? Could it be because of the '~' character in the middle?

Whichever way, to check for the last character, simply do

unichar last = [urlString characterAtIndex:urlString.length - 1];
if (last == '/')
    urlString = [urlString substringToIndex:urlString.length - 1];

Upvotes: 9

Related Questions