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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28059543/
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-31 04:26:15  来源:igfitidea点击:

Swift Replace Multiple Characters in String

iosstringswiftreplace

提问by Jeff Richard

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 中断标记的以下代码行。但是,我还有其他 HTML 符号需要替换,当我再次调用这行代码时,使用不同的参数,就好像第一个被覆盖了一样。有没有办法可以包含多个参数?在 Swift 中是否有更有效的方法来做到这一点?例如:将 br> 替换为“”,将 nbsp 替换为“”。

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

回答by Michael Peterson

Use replacingOccurrencesalong with a the String.CompareOptions.regularExpresionoption.

使用replacingOccurrences用了一起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]

输入要在方括号内替换的字符,如下所示 [\\ Characters]

Output:

输出:

7Hello, 7play7ground777

回答by skofgar

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(
let str = "Hello, stackoverflow"
let chars: [Character] = ["a", "e", "i"]
let myStrings = ["Hello", ", ", "overflow"]

let newString = str.stringByRemovingAll(chars)
let anotherString = str.stringByRemovingAll(myStrings)
) })) } func stringByRemovingAll(subStrings: [String]) -> String { var resultString = self subStrings.map { resultString = resultString.stringByReplacingOccurrencesOfString(
let result1 = content.stringByReplacingOccurrencesOfString("<br /><br />", withString:"\r") 
let result2 = result1.stringByReplacingOccurrencesOfString(" &nbsp;", withString:" ")
textView.text = result2
, withString: "") } return resultString } }

Example:

例子:

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

Result, when printed:

结果,打印时:

newString: Hllo, stckovrflow

新字符串: Hllo, stckovrflow

anotherString: stack

另一个字符串: stack

回答by Jawwad

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

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

Something like this should work for you

像这样的东西应该适合你

##代码##

回答by Leo Dabus

##代码##