When you look at the docs for
String.index(_:offsetBy:limitedBy:), you get this description: Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index.
Continue reading …
Gordon Fontenot published his StringTemplate library the other day. You can use it as a much better number formatter, for example:
- “5125550001” is your user input
- “(XXX) XXX-XXXX” is your template
- “(512) 555-0001” is the result of applying the template
There are options to ignore overflow or crop the remainder when only inserting part of the required digits. I think that’s pretty slick!