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.9k Golang : Covert map/slice/array to JSON or XML format
+7.7k Golang : Get today's weekday name and calculate target day distance example
+16.1k Golang : Loop each day of the current month example
+9.6k Golang : Find correlation coefficient example
+15.1k Golang : How to get Unix file descriptor for console and file
+8.1k Golang : Configure Apache and NGINX to access your Go service example
+7.7k Golang : Regular Expression find string example
+16.6k Golang : Gzip file example
+6.8k Golang : Decode XML data from RSS feed
+8.8k Golang : Sort lines of text example
+9.5k Golang : How to generate Code 39 barcode?
+23.4k Golang : Check if element exist in map