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
+3.8k Golang : How to pass data between controllers with JSON Web Token
+19.9k Golang : Sort and reverse sort a slice of strings
+6.5k Golang : Handling Yes No Quit query input
+9.1k Golang : Get escape characters \u form from unicode characters
+17.9k Golang : Get RGBA values of each image pixel
+19k Golang : How to get struct tag and use field name to retrieve data?
+7.4k Android Studio : Import third-party library or package into Gradle Scripts
+17.4k Golang : Generate thumbnails from images
+8.5k Golang : Get all countries currencies code in JSON format
+39k Golang : How do I convert int to uint8?
+10.1k Golang : Concatenate (combine) buffer data example
+21k Golang : Read directory content with filepath.Walk()