如何替换UITextView中的特定字符

问题描述

在日志窗口中您好,我有两个数组。第一个是我的彩色数组,您可以看到模拟器蓝色的。我想用第二个数组更改/替换蓝色,但它们是我的和弦。

Screenshot

实际上,我尝试按照以下代码进行操作,但是所有蓝色文本均更改为第二个数组的第一个字符串“ Dbm”。

for txt in coloredTexts {

    for transposed in transposedAkorArray{
        
        self.akor_goster.text = self.akor_goster.text.replacingOccurrences(of: "\(txt)",with: "\(transposed)")
    }
    
}

解决方法

尝试

for (index,txt) in coloredTexts.enumerated() {
    let range = akor_goster.text.range(of: txt)
    akor_goster.text.replacingOccurrences(of: txt,with: transposedAkorArray[index],options: [],range: range)
}
,

我认为您正在尝试执行以下操作:

for i in 0..<coloredTexts.count {
    self.akor_goster.text = self.akor_goster.text.replacingOccurrences(of: "\(coloredTexts[i])",with: "\(transposedAkorArray[i])")
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...