Kishore Reddy
Kishore Reddy

Reputation: 926

How to replace more than one common sub string in a single string with another string?

    for(int i= 0 ;i<[urlsArrray count]; i++)
    {
        NSString *urlString = [urlsArrray objectAtIndex:i];
        NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:escapedUrlString];

        NSString *urlstring1 = [url absoluteString];
        NSArray *parts = [urlstring1 componentsSeparatedByString:@"/"];
        NSString *fileName = [parts objectAtIndex:[parts count]-1];

        NSMutableString *tempString = [NSMutableString stringWithString:fileName];

       // [tempString replaceCharactersInRange:[tempString rangeOfString:@"%20"] withString:@" "];

                NSLog(@"file name in temp string: %@ word name: %@", tempString, wordNameDB);

                NSRange match = [tempString rangeOfString:wordNameDB];
                if(match.location != NSNotFound)
                {
                    NSLog(@"match found at %u", match.location);
                    isAvailable = YES;
                    break;
                }

Hi friends, now my problem is i am getting file name from server..., if file name is having any spaces then it replace '%20' ( i.e ex: "hello world" is actual name but i am getting file name like: "hello%20world") . 1. I am not sure all file names having spaces. 2. And also i am not sure a file may have only one space

so first i have to check the file is having spaces or not, if have then i want to replace all "%20" with @" " string. Please give me any suggestions or code snippets.

OR " THERE IA ANY OTHER WAY TO READ FILE NAMES WITHOUT GETTING '%20' IN THE PLACE OF SPACE(@" ")..... thank you

Upvotes: 2

Views: 142

Answers (5)

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81878

You seem to already have a valid NSURL object representing the file. Getting the filename from a URL is easy:

...
NSURL *url = [NSURL URLWithString:escapedUrlString];
NSString *path = [url path];
NSString *filename = [path lastPathComponent];

No fiddling with unescaping percent escapes, URL parsing, and other error prone stuff.

Upvotes: 0

iDev
iDev

Reputation: 23278

If you have your file name stored in fileName param, you can use the following:

fileName = [fileName stringByReplacingOccurrencesOfString:@"%20" withString:@" "];

The above code will replace all "%20" with " ". If there are no "%20" in the fileName, you will get back the same string.

Correction: I was confused with stringByAddingPercentEscapesUsingEncoding mentioned in code and thought you have already used stringByReplacingPercentEscapesUsingEncoding. If you are not using stringByReplacingPercentEscapesUsingEncoding method, you should use that in this case. The above code is useful, only if that is not able to remove any particular string which you want to replace.

Upvotes: 2

Mage
Mage

Reputation: 151

Use this to remove spaces ..

urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

Upvotes: 1

Artem Shmatkov
Artem Shmatkov

Reputation: 1433

THERE IA ANY OTHER WAY TO READ FILE NAMES WITHOUT GETTING '%20' IN THE PLACE OF SPACE(@" ")

Yes, use this:

NSString *newString = [yourstring stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Upvotes: 1

Vlad
Vlad

Reputation: 3366

What you need is replacing the escape charcters, according to the encoding. Use this and all your spaces and other URL encoded characters will be converted to what you need.

[@"yourString" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Upvotes: 1

Related Questions