簡體   English   中英

Swift字符串編碼

[英]Swift string encoding

我的字符串帶有 ,我想用百分比編碼( %26 )替換它。 這只是一個例子。 可以有任何轉義字符。
我試過了,但它不能用%26代替

password.text?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)

這個短字符串擴展如何:

extension String
{
    func encode(_ chars: String) -> String
    {
        let forbidden = CharacterSet(charactersIn: chars)
        return self.addingPercentEncoding(withAllowedCharacters: forbidden.inverted) ?? self
    }
}

print("I can't read ENCODED text".encode("CDENO"))
// will print: I can't read %45%4E%43%4F%44%45%44 text

print("Command & Conquer".encode("&"))
// will print: Command %26 Conquer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM