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 Golang : Count leading or ending zeros(any item of interest) example
+10k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+38.6k Golang : How to iterate over a []string(array)
+5.8k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+40.8k Golang : How do I convert int to uint8?
+18.4k Golang : Delete duplicate items from a slice/array
+5.4k Golang : Struct field tags and what is their purpose?
+4.4k Golang : PGX CopyFrom to insert rows into Postgres database
+7.4k Setting $GOPATH environment variable for Unix/Linux and Windows
+6.4k Android Studio : Hello World example
+17.6k Golang : Convert IPv4 address to decimal number(base 10) or integer