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
+8.3k Golang : How To Use Panic and Recover
+4.4k Javascript : How to show different content with noscript?
+5.9k Unix/Linux : Get reboot history or check when was the last reboot date
+17.9k Golang : Iterate linked list example
+10.2k Golang : How to tokenize source code with text/scanner package?
+13.7k Golang : reCAPTCHA example
+8.1k Golang : Trim everything onward after a word
+6.4k Golang : Calculate US Dollar Index (DXY)
+21.9k SSL : How to check if current certificate is sha1 or sha2
+9.2k Golang : Gonum standard normal random numbers example
+20.6k nginx: [emerg] unknown directive "passenger_enabled"
+14.8k Golang : Reset buffer example