Swift替换字符串中的多个字符

时间:2021-02-22 01:59:18

Below is the following line of code I use to replace an HTML break tag with a carriage return. However, I have other HTML symbols that I need to replace and when I call this line of code again, with different parameters, it's as if the first one is overwritten. Is there a way I can include multiple parameters? Is there a more efficient way to do this in Swift? For example: replace both br> with "" and nbsp with "".

下面是我用来用回车符替换HTML break标记的以下代码行。但是,我还需要替换其他HTML符号,当我再次调用这行代码时,使用不同的参数,就像第一个被覆盖一样。有没有办法可以包含多个参数?在Swift中有更有效的方法吗?例如:将br>替换为“”,将nbsp替换为“”。

textView.text = content.stringByReplacingOccurrencesOfString("<br /><br />", withString:"\r")

4 个解决方案

#1


12  

Use replacingOccurrences along with a the String.CompareOptions.regularExpresion option.

使用replacementOccurrences和String.CompareOptions.regularExpresion选项。

Example (Swift 3):

示例(Swift 3):

var x = "<Hello, [play^ground+]>"
let y = x.replacingOccurrences(of: "[\\[\\]^+<>]", with: "7", options: .regularExpression, range: nil)
print(y)

Input characters which are to be replaced inside the square brackets like so [\\ Characters]

在方括号内输入要输入的字符,如[\\字符]

Output:

输出:

7Hello, 7play7ground777

#2


7  

I solved it based on the idea of Rosetta Code

我根据Rosetta Code的想法解决了这个问题

extension String {
    func stringByRemovingAll(characters: [Character]) -> String {
        return String(self.characters.filter({ !characters.contains($0) }))
    }

    func stringByRemovingAll(subStrings: [String]) -> String {
        var resultString = self
        subStrings.map { resultString = resultString.stringByReplacingOccurrencesOfString($0, withString: "") }
        return resultString
    }
}

Example:

例:

let str = "Hello, *"
let chars: [Character] = ["a", "e", "i"]
let myStrings = ["Hello", ", ", "overflow"]

let newString = str.stringByRemovingAll(chars)
let anotherString = str.stringByRemovingAll(myStrings)

Result, when printed:

结果,打印时:

newString: Hllo, stckovrflow

newString:Hllo,stckovrflow

anotherString: stack

anotherString:stack

#3


6  

As @matt mentioned you are starting over with the same content string. The stringByReplacingOccurrencesOfString method doesn't actually change anything in the original content string. It returns to you a new string with the replacement changes while content remains unchanged.

正如@matt所提到的,你将重新开始使用相同的内容字符串。 stringByReplacingOccurrencesOfString方法实际上不会更改原始内容字符串中的任何内容。当内容保持不变时,它会返回一个包含替换更改的新字符串。

Something like this should work for you

这样的事情对你有用

let result1 = content.stringByReplacingOccurrencesOfString("<br /><br />", withString:"\r") 
let result2 = result1.stringByReplacingOccurrencesOfString(" &nbsp;", withString:" ")
textView.text = result2

#4


1  

extension String {
    var html2AttributedString:NSAttributedString {
        return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil, error: nil)!
    }
}

let myHtmlCode = "<style type=\"text/css\">#red{color:#F00}#green{color:#0F0}#blue{color: #00F}</style><span id=\"red\" >Red</span> <span id=\"green\" >Green</span><span id=\"blue\">Blue</span>"

myHtmlCode.html2AttributedString

#1


12  

Use replacingOccurrences along with a the String.CompareOptions.regularExpresion option.

使用replacementOccurrences和String.CompareOptions.regularExpresion选项。

Example (Swift 3):

示例(Swift 3):

var x = "<Hello, [play^ground+]>"
let y = x.replacingOccurrences(of: "[\\[\\]^+<>]", with: "7", options: .regularExpression, range: nil)
print(y)

Input characters which are to be replaced inside the square brackets like so [\\ Characters]

在方括号内输入要输入的字符,如[\\字符]

Output:

输出:

7Hello, 7play7ground777

#2


7  

I solved it based on the idea of Rosetta Code

我根据Rosetta Code的想法解决了这个问题

extension String {
    func stringByRemovingAll(characters: [Character]) -> String {
        return String(self.characters.filter({ !characters.contains($0) }))
    }

    func stringByRemovingAll(subStrings: [String]) -> String {
        var resultString = self
        subStrings.map { resultString = resultString.stringByReplacingOccurrencesOfString($0, withString: "") }
        return resultString
    }
}

Example:

例:

let str = "Hello, *"
let chars: [Character] = ["a", "e", "i"]
let myStrings = ["Hello", ", ", "overflow"]

let newString = str.stringByRemovingAll(chars)
let anotherString = str.stringByRemovingAll(myStrings)

Result, when printed:

结果,打印时:

newString: Hllo, stckovrflow

newString:Hllo,stckovrflow

anotherString: stack

anotherString:stack

#3


6  

As @matt mentioned you are starting over with the same content string. The stringByReplacingOccurrencesOfString method doesn't actually change anything in the original content string. It returns to you a new string with the replacement changes while content remains unchanged.

正如@matt所提到的,你将重新开始使用相同的内容字符串。 stringByReplacingOccurrencesOfString方法实际上不会更改原始内容字符串中的任何内容。当内容保持不变时,它会返回一个包含替换更改的新字符串。

Something like this should work for you

这样的事情对你有用

let result1 = content.stringByReplacingOccurrencesOfString("<br /><br />", withString:"\r") 
let result2 = result1.stringByReplacingOccurrencesOfString(" &nbsp;", withString:" ")
textView.text = result2

#4


1  

extension String {
    var html2AttributedString:NSAttributedString {
        return NSAttributedString(data: dataUsingEncoding(NSUTF8StringEncoding)!, options:[NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding], documentAttributes: nil, error: nil)!
    }
}

let myHtmlCode = "<style type=\"text/css\">#red{color:#F00}#green{color:#0F0}#blue{color: #00F}</style><span id=\"red\" >Red</span> <span id=\"green\" >Green</span><span id=\"blue\">Blue</span>"

myHtmlCode.html2AttributedString