Replace occurrences of space in URL

55,056

Solution 1

The correct format for replacing space from url is :

Swift 4.2 , Swift 5

var urlString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

Swift 4

var urlString = originalString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)

Objective C

NSString *urlString;//your url string.

urlString = [originalUrl stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

or

urlString = [originalUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

iOS 9 and later

urlString = [originalUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

Solution 2

Swift 2.0

let originalUrl = "http://myurl.com/my photo.png"
let urlNew:String = urlReq.stringByAddingPercentEncodingWithAllowedCharacters( NSCharacterSet.URLQueryAllowedCharacterSet())! 

Output:

http://myurl.com/my%20photo.png

Solution 3

To replace occurence in SWIFT 3 :

let updatedUrl = originalUrl.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)

Solution 4

Swift 4

Another way to replace an empty space with replacingOccurrences method:

let yourString = "http://myurl.com/my photo.png"
let urlNew:String = yourString.replacingOccurrences(of: " ", with: "%20").trimmed 

That will replace the empty space (" ") with '%20'

Solution 5

Swift 5

var urlString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
Share:
55,056

Related videos on Youtube

Joy
Author by

Joy

Updated on June 20, 2020

Comments

  • Joy
    Joy almost 4 years

    I have a URL in an iPhone application to work with. But the problem is that it has some spaces in the URL. I want to replace the spaces with '%20'. I know that there are the stringByReplacingOccurencesOfString and stringByAddingPercentEscapesUsingEncoding methods. I also have used them. But they are not working for me. The spaces are replaced by some unusual values.

    I'm applying those methods on an instance of NSString.

    • Tieme
      Tieme almost 13 years
      Check out this post here: stackoverflow.com/questions/695911/… The Answer: NSString* escapedUrl = [originalUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncodi‌​ng];
  • Raj
    Raj about 9 years
    @RaquibulIslam I edited my answer , please check now
  • girish_vr
    girish_vr about 8 years
    I think stringByReplacingPercentEscapesUsingEncoding() is deprecated. Instead, we can use stringByRemovingPercentEncoding
  • Neuron
    Neuron over 6 years
    welcome to SO. please improve the quality of you answer
  • Sabrina
    Sabrina over 5 years
    error : Value of type 'String' has no member 'trim'
  • Gilad Brunfman
    Gilad Brunfman over 5 years
    @Mehdico that is odd, I always trim my strings. At this example you don’t have to trim the string since you know what it is. But I’d clean the project and try to run it again. at the end of the string add .trimmed
  • Sabrina
    Sabrina over 5 years
    dude i think you use an extension for String class, because .trimmed is not found in swift 4 and 4.2
  • Gilad Brunfman
    Gilad Brunfman over 5 years
    You don’t have to trim the string in order to replace white space
  • wesley franks
    wesley franks almost 5 years
    You need to give more of an explanation to this code. There are others coming to this question looking for understanding not just code. Help others as well.
  • shiv
    shiv over 4 years
    Doesn't work as URL has no member 'replacingOccurences'. String has that method but URL doesn't.
  • Shrikant Phadke
    Shrikant Phadke about 4 years
    Try this for encoding :- stringByAddingPercentEncodingWithAllowedCharacters: URLPathAllowedCharacterSet and for decoding :- [NSURL fileURLWithPath:[self.yourString stringByRemovingPercentEncoding]]
  • guru
    guru almost 4 years
    will it work for local path? i mean for documents dir
  • guru
    guru almost 4 years
    will it work for local path? i mean for documents dir
  • guru
    guru almost 4 years
    will it work for local path? i mean for documents dir
  • guru
    guru almost 4 years
    will it work for local path? i mean for documents dir
  • Raj
    Raj over 3 years
    @guru I don't think you need to format path for local document dir