6

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
  • NSRegularExpression, NSScanner, rangeOf (if find the /) is clearly enough... Commented Dec 7, 2017 at 9:31

1 Answer 1

16

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:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.