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
+10.1k Golang : Convert octal value to string to deal with leading zero problem
+6.8k Golang : Output or print out JSON stream/encoded data
+5k Python : Convert(cast) bytes to string example
+12.8k Golang : flag provided but not defined error
+21.8k Golang : GORM create record or insert new record into database example
+12.5k Elastic Search : Return all records (higher than default 10)
+25.3k Golang : Storing cookies in http.CookieJar example
+8.3k Golang : Reverse text lines or flip line order example
+13.5k Golang : Linear algebra and matrix calculation example
+14.5k Golang : How to filter a map's elements for faster lookup
+10.6k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter