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
+29.3k Golang : missing Git command
+18k Golang : Simple client server example
+12.4k Golang : List running EC2 instances and descriptions
+7.8k Android Studio : AlertDialog to get user attention example
+22.9k Golang : Set and Get HTTP request headers example
+19.4k Golang : Get RGBA values of each image pixel
+6.7k Golang : Totalize or add-up an array or slice example
+7.3k Golang : Use modern ciphers only in secure connection
+11.5k Golang : Fix fmt.Scanf() on Windows will scan input twice problem
+13.9k Golang : Check if an integer is negative or positive
+6.6k Unix/Linux : How to get own IP address ?
+5.9k Linux : Disable and enable IPv4 forwarding