Replace occurrences of space in URL

IosObjective CNsstringNsurl

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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 - Ios

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 - Ios

To replace occurence in SWIFT 3 :

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

Solution 4 - Ios

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 - Ios

Swift 5

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

Solution 6 - Ios

Swift 5.3, clear space your string,

let str = "  www.test.com  "
let trimmed = str.trimmingCharacters(in: .whitespacesAndNewlines)
print(str) // "www.test.com" 

Solution 7 - Ios

var urlString :String = originalUrl.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!

Solution 8 - Ios

Hope this will work

let url = "https:youtube.56432fgrtxcvfname=xyz&sname=tuv"
let urlNew:String = url.replacingOccurrences(of: " ", with: "%20")
Alamofire.request(urlNew, method: .get, headers: headers).responseJSON{
response in
print(response)    
}

It will remove all kind of spaces from the url.

Solution 9 - Ios

SWIFT 3.1

Simple way to replace an empty space with replacingOccurrences:

URL = URL.replacingOccurrences(of: " ", with: "", options: .literal, range: nil)

Solution 10 - Ios

Swift 4, iOS-9

let **urlSearchVal**:String = "top 10 movies"     
let urlString = 
                              
    "https://www.googleapis.com/youtube/v3/search?part=snippet&q=\(urlSearchVal)&key=......&type=video"   
//replace ...... above with your youtube key   
// to ignoring white space in search  
        let UrlString :String = urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)!

Solution 11 - Ios

A Swift 4 solution. You simply pass through a string and it fills spaces with %20 and adds "http://" to the beginning to the string. Pretty sweet!

URL(fileURLWithPath: String) 

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoyView Question on Stackoverflow
Solution 1 - IosRajView Answer on Stackoverflow
Solution 2 - IosDasogaView Answer on Stackoverflow
Solution 3 - IosJitendra TanwarView Answer on Stackoverflow
Solution 4 - IosGilad BrunfmanView Answer on Stackoverflow
Solution 5 - IosShakeel AhmedView Answer on Stackoverflow
Solution 6 - IosikbalView Answer on Stackoverflow
Solution 7 - IossamaujsView Answer on Stackoverflow
Solution 8 - IosRaghib ArshiView Answer on Stackoverflow
Solution 9 - IosShibin k kurianView Answer on Stackoverflow
Solution 10 - Iosthat guyView Answer on Stackoverflow
Solution 11 - IosKDiazView Answer on Stackoverflow