ios 替换 URL 中出现的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3439853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:31:30  来源:igfitidea点击:

Replace occurrences of space in URL

iosobjective-cnsstringnsurl

提问by Joy

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 stringByReplacingOccurencesOfStringand stringByAddingPercentEscapesUsingEncodingmethods. I also have used them. But they are not working for me. The spaces are replaced by some unusual values.

我在 iPhone 应用程序中有一个 URL 可以使用。但问题是它在 URL 中有一些空格。我想用“%20”替换空格。我知道有stringByReplacingOccurencesOfStringstringByAddingPercentEscapesUsingEncoding方法。我也用过它们。但他们不为我工作。空格被一些不寻常的值取代。

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

我正在将这些方法应用于NSString.

回答by Raj

The correct format for replacing space from url is :

从 url 替换空格的正确格式是:

Swift 4.2 , Swift 5

斯威夫特 4.2 , 斯威夫特 5

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

Swift 4

斯威夫特 4

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

Objective C

目标 C

NSString *urlString;//your url string.

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

or

或者

urlString = [originalUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

iOS 9 and later

iOS 9 及更高版本

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

回答by Dasoga

Swift 2.0

斯威夫特 2.0

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

Output:

输出:

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

回答by Jitendra Tanwar

To replace occurence in SWIFT 3:

替换SWIFT 3 中的出现

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

回答by Gilad Brunfman

Swift 4

斯威夫特 4

Another way to replace an empty space with replacingOccurrences method:

另一种用replaceOccurrences方法替换空白空间的方法:

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'

这将用 '%20' 替换空白空间 (" ")

回答by Shakeel Ahmed

Swift 5

斯威夫特 5

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

回答by Raghib Arshi

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.

它将从 url 中删除所有类型的空格。

回答by samaujs

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

回答by KDiaz

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!

Swift 4 解决方案。您只需传递一个字符串,它会用 %20 填充空格并将“http://”添加到字符串的开头。好甜!

URL(fileURLWithPath: String) 

回答by Shibin k kurian

SWIFT 3.1

斯威夫特 3.1

Simple way to replace an empty space with replacingOccurrences:

用replaceOccurrences 替换空白空间的简单方法:

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

回答by that guy

Swift 4, iOS-9

斯威夫特 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)!