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
+11.9k Golang : How to display image file or expose CSS, JS files from localhost?
+6.4k Default cipher that OpenSSL used to encrypt a PEM file
+7.8k Golang : Metaprogramming example of wrapping a function
+11.4k Golang : Find and draw contours with OpenCV example
+9.5k Golang : Get current, epoch time and display by year, month and day
+9.4k Golang : List available AWS regions
+14.7k Golang : How do I get the local IP (non-loopback) address ?
+4.5k Javascript : How to get width and height of a div?
+4.3k MariaDB/MySQL : How to get version information
+20.3k Golang : Convert PNG transparent background image to JPG or JPEG image
+9.3k Golang : Format strings to SEO friendly URL example
+10k Android Studio : Simple input textbox and intercept key example