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
+16.2k Golang : Convert slice to array
+13.1k Golang : Linear algebra and matrix calculation example
+23k Golang : Print out struct values in string format
+5.5k Golang : Struct field tags and what is their purpose?
+37.2k Upload multiple files with Go
+10k Golang : How to profile or log time spend on execution?
+16k Golang : convert string or integer to big.Int type
+20.2k Nginx + FastCGI + Go Setup.
+24.2k Golang : Time slice or date sort and reverse sort example
+14.7k Golang : Search folders for file recursively with wildcard support
+7.5k Golang : get the current working directory of a running program
+11.8k Golang : Convert a rune to unicode style string \u