Skip to content

Instantly share code, notes, and snippets.

@NickAger
Last active November 8, 2017 15:54
Show Gist options
  • Save NickAger/13b31868e16f37233c6f6be440e9629f to your computer and use it in GitHub Desktop.
Save NickAger/13b31868e16f37233c6f6be440e9629f to your computer and use it in GitHub Desktop.
Understanding Swifts substringToIndex & substringFromIndex
"hello".substringToIndex(1) // "h"
// s.substringToIndex(1) // String may not be indexed with 'Int', it has variable size elements
s.substringToIndex(s.startIndex.advancedBy(1)) // "h"
s.substringFromIndex(s.startIndex.advancedBy(1)) // "ello"
s.substringFromIndex(s.endIndex.advancedBy(-1)) // "o"
s.substringToIndex(s.endIndex.advancedBy(-1)) // "hell"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment