Swift : substringWithRange() function example
A quick tutorial as add on from previous tutorial on how to get sub-string with rangeOfString() function. Another way to get sub string in Swift is to use the substringWithRange()
function.
For example :
var str = "Hello World!"
str.substringWithRange(Range<String.Index>(start: str.startIndex, end: str.endIndex))
Output :
"Hello World!"
and if you want to start from position 3 and position -1 from end. Change the start
and end
position with advance()
function.
var str = "Hello World!"
str.substringWithRange(Range<String.Index>(start: advance(str.startIndex, 3), end: advance(str.endIndex, -1)))
Output :
"lo World"
See also : Swift : Get substring with rangeOfString() function example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.7k Golang : Output or print out JSON stream/encoded data
+10.6k Golang : Flip coin example
+16.4k Golang : How to implement two-factor authentication?
+8.2k Golang : Reverse text lines or flip line order example
+9.2k Golang : How to find out similarity between two strings with Jaro-Winkler Distance?
+26.8k Golang : Find files by extension
+17.7k Golang : Defer function inside init()
+3.4k Golang : Fix go-cron set time not working issue
+24k Golang : Find biggest/largest number in array
+6.7k Golang : Skip or discard items of non-interest when iterating example
+17.5k Golang : Linked list example