如何获得属性字符串的字符串范围?

Tec*_*ain 1 range nsattributedstring ios swift swift4

我有字符串作为2/42。这里2和42是动态的,现在我想设置/42为字符串的上标。我无法找到的范围/42。请告诉我如何迅速找到范围?

Kru*_*nal 5

属性字符串的可变字符串具有查找特定字符串范围的功能。

试试看,看看(Swift 4):

let stringValue = "2/42"
let attributedString: NSMutableAttributedString = NSMutableAttributedString(string: stringValue)
let range: NSRange = attributedString.mutableString.range(of: "/42", options: .caseInsensitive)
print("range - \(range)")
Run Code Online (Sandbox Code Playgroud)

结果:
范围-{1,3}

参考快照:

在此处输入图片说明