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
+10.1k Golang : Find age or leap age from date of birth example
+4.6k Unix/Linux/MacOSx : Get local IP address
+4.2k PHP : See installed compiled-in-modules
+6.4k Javascript : Push notifications to browser with Push.js
+5.7k Get Facebook friends working in same company
+5.1k Golang : Convert Chinese UTF8 characters to Pin Yin
+8.4k Golang : Quadratic example
+29.6k error: trying to remove "yum", which is protected
+18.3k Golang : Compare floating-point numbers
+5.1k Golang : Missing Subversion command
+7.4k Golang : On lambda, anonymous, inline functions and function literals
+44.3k Golang : Marshal and unmarshal json.RawMessage struct example