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
+8k Prevent Write failed: Broken pipe problem during ssh session with screen command
+10.7k Golang : How to transmit update file to client by HTTP request example
+5.3k Python : Print unicode escape characters and string
+8.7k Golang : Get SPF and DMARC from email headers to fight spam
+20.2k Nginx + FastCGI + Go Setup.
+11.6k Golang : Find and draw contours with OpenCV example
+10.4k Golang : Underscore string example
+5.8k Golang : Grab news article text and use NLP to get each paragraph's sentences
+9.8k CodeIgniter : Load different view for mobile devices
+31.7k Golang : Validate email address with regular expression
+13.2k Golang : Count number of runes in string
+5.9k Golang : How to write backslash in string?