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
+19.7k Golang : Print out struct values in string format
+3.5k Golang : Join lines with certain suffix symbol example
+3.1k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+3.3k Python : Convert(cast) string to bytes example
+25.2k Golang : missing Git command
+5.2k Golang : How to capture return values from goroutines?
+2.7k Javascript : Access JSON data example
+4.1k Golang : Skip or discard items of non-interest when iterating example
+9.9k Golang : Get uploaded file name or access uploaded files
+6.5k Golang : Take screen shot of browser with JQuery example
+22.4k Golang : How to write CSV data to file
+5.3k Golang : Individual and total number of words counter example