user3647675 user3647675 - 4 months ago 25
Objective-C Question

How to take NSRange in swift?

I am very much new to swift language. I am performing some business logic which needs to take

NSRange
from given String.

Here is my requirement,
Given Amount = "144.44"
Need
NSRange
of only cent part i.e. after "."

Is there any API available for doing this?

Answer

You can do a regex-based search to find the range:

let str : NSString = "123.45"
let rng : NSRange = str.range("(?<=[.])\\d*$", options: .RegularExpressionSearch)

Regular expression "(?<=[.])\\d*$" means "zero or more digits following a dot character '.' via look-behind, all the way to the end of the string $."