I have string as 2/42. Here 2 & 42 is dynamic.Now I want to set /42 as superscript of string. I am not able to find out the range of /42. please tell me how do i find out the range in swift?
1 Answer
Mutable string of attributed string has functionality to find of range of specific string.
Try this and see (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)")
Result:
range - {1, 3}
Ref Snapshot:

NSRegularExpression,NSScanner,rangeOf(if find the/) is clearly enough...