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
+22k Golang : Read directory content with filepath.Walk()
+21.1k Golang : Encrypt and decrypt data with TripleDES
+7.6k Javascript : How to check a browser's Do Not Track status?
+30.5k Golang : Interpolating or substituting variables in string examples
+4.4k MariaDB/MySQL : How to get version information
+5.1k Unix/Linux/MacOSx : How to remove an environment variable ?
+7.3k Javascript : Push notifications to browser with Push.js
+4.6k Golang : A program that contain another program and executes it during run-time
+10.5k Golang : Get UDP client IP address and differentiate clients by port number
+27.7k Golang : Move file to another directory
+4.2k Golang : Valued expressions and functions example
+11.3k Golang : Simple file scaning and remove virus example