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)
Related videos on Youtube
Comments
-
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
andstringByAddingPercentEscapesUsingEncoding
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 almost 13 yearsCheck out this post here: stackoverflow.com/questions/695911/… The Answer: NSString* escapedUrl = [originalUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
-
-
Raj about 9 years@RaquibulIslam I edited my answer , please check now
-
girish_vr about 8 yearsI think stringByReplacingPercentEscapesUsingEncoding() is deprecated. Instead, we can use stringByRemovingPercentEncoding
-
Neuron over 6 yearswelcome to SO. please improve the quality of you answer
-
Sabrina over 5 yearserror : Value of type 'String' has no member 'trim'
-
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 over 5 yearsdude i think you use an extension for String class, because .trimmed is not found in swift 4 and 4.2
-
Gilad Brunfman over 5 yearsYou don’t have to trim the string in order to replace white space
-
wesley franks almost 5 yearsYou 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 over 4 yearsDoesn't work as URL has no member 'replacingOccurences'. String has that method but URL doesn't.
-
Shrikant Phadke about 4 yearsTry this for encoding :- stringByAddingPercentEncodingWithAllowedCharacters: URLPathAllowedCharacterSet and for decoding :- [NSURL fileURLWithPath:[self.yourString stringByRemovingPercentEncoding]]
-
guru almost 4 yearswill it work for local path? i mean for documents dir
-
guru almost 4 yearswill it work for local path? i mean for documents dir
-
guru almost 4 yearswill it work for local path? i mean for documents dir
-
guru almost 4 yearswill it work for local path? i mean for documents dir
-
Raj over 3 years@guru I don't think you need to format path for local document dir